gpt4 book ai didi

java - 为什么我的数据包没有发送到我的其他 Activity ?

转载 作者:行者123 更新时间:2023-12-02 04:57:36 26 4
gpt4 key购买 nike

一旦用户单击“下订单”按钮,我就会尝试将一些数据从一个 Activity 传递到第二个 Activity 。我希望数据显示在第二个 Activity 上,但它给了我空值或零值。唯一的解释是,通过 bundle 传递的数据没有被设置,或者没有被传递。我创建了一个名为 size 的全局字符串变量,并将其设置为 xLarge,以尝试找到此错误所在。当我运行该程序时,它在第二个 Activity 中返回空值。有谁知道为什么?这是带有一些照片的代码 enter image description here

enter image description here

Activity 1

    package com.example.switchviews;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class Activity1 extends Activity {
/** Called when the activity is first created. */
EditText mEdit;
EditText mEdit2;
double amount;
double small;
double meduium;
double large;
int count = 0;
int numToppings;
double toppingsTot=0;
Button next;
String size ="xLarge";
double sizePrice;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

Intent myIntent = new Intent(Activity1.this, Activity2.class);
Bundle myData = new Bundle();
myData.putDouble("price", amount);
myData.putDouble("toppings", toppingsTot);
myData.putInt("numberOfToppings", numToppings);
myData.putDouble("size", sizePrice);
// Why is this set to null in activity 2?
myData.putString("sizePrice", size);
int[] myLittleArray = {1, 2, 3, 4,5,6, 7};
myData.putIntArray("myIntArray1", myLittleArray);
myIntent.putExtras(myData);
startActivityForResult(myIntent,2);
}




});
}

Activity 2

    package com.example.switchviews;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Activity2 extends Activity {

TextView pizza;
TextView toppings;
TextView total;
Button submit;
Button cancel;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
pizza = (TextView)findViewById(R.id.pizza);
toppings = (TextView) findViewById(R.id.toppings);
total = (TextView)findViewById(R.id.total);
submit = (Button)findViewById(R.id.Button02);
cancel = (Button)findViewById(R.id.Button03);

Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();

double amount = myBundle.getDouble("price");
double toppingsTot = myBundle.getDouble("toppings");
int numToppings = myBundle.getInt("numberOfToppings");
double sizePrice = myBundle.getDouble("sizePrice");
String size = myBundle.getString("size");
int[] arr1 = myBundle.getIntArray("myIntArray1");

pizza.setText("Pizza "+ size + " 1x" + Double.toString(sizePrice)+ " "+ " "+ Double.toString(sizePrice) );


Button next = (Button) findViewById(R.id.Button03);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}

});
}

}

list XML

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.switchviews"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.switchviews.Activity1"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2"></activity>
</application>

</manifest>

最佳答案

为什么在 Activity 2 中将其设置为 null?

由于关键数据类型不匹配。从第一个 Activity 发送 sizePrice 作为字符串,但尝试获取 double 而不是字符串。这样做:

double sizePrice = myBundle.getDouble("size");
String size = myBundle.getString("sizePrice"); // return xLarge

关于java - 为什么我的数据包没有发送到我的其他 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28620688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com