gpt4 book ai didi

java - Android Intent 不工作(将数据发送到 Activity 并接收回来)

转载 作者:行者123 更新时间:2023-11-30 01:07:29 24 4
gpt4 key购买 nike

我想将两个 EditText 数据发送到下一个 Activity ,并在单击按钮时也将其取回。但每当我通过 Intent 发送数据时,我的 Activity 就会崩溃。我从昨天就卡在了...

InvoiceActivity.java

public class InvoiceActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invoice);

//Getting values from calendar activity

Bundle bundleCal = getIntent().getExtras();

data123 = bundleCal.getString("stuff1");//date
EditText et123 = (EditText) findViewById(R.id.editText4);
et123.setText(data123);

String a = bundleCal.getString("stuff11");//contact no
EditText et1 = (EditText) findViewById(R.id.editText1);
et1.setText(a);
et1.setEnabled(false);

String b = bundleCal.getString("stuff22");//name
EditText et2 = (EditText) findViewById(R.id.editText2);
et2.setText(b);
et2.setEnabled(false);

//Getting values from Fourth activity(the activity from where contact no. and name are previously coming)

//Get the bundle
Bundle bundle = getIntent().getExtras();

//Extract the data…
String data11 = bundle.getString("stuff11");//contact no
EditText et11 = (EditText) findViewById(R.id.editText1);
et11.setText(data11);
et11.setEnabled(false);

String data22 = bundle.getString("stuff22");//name
EditText et22 = (EditText) findViewById(R.id.editText2);
et22.setText(data22);
et22.setEnabled(false);

EditText oid = (EditText) findViewById(R.id.editText3);
oid.setEnabled(false);

EditText rem1 = (EditText) findViewById(R.id.editText7);//Remaining
rem1.setEnabled(false);

int min = 1;
int max = 99;

Random r = new Random();
int id = r.nextInt(max - min + 1) + min;

String ooid=id+"";
oid.setText(ooid);

EditText dd11 = (EditText) findViewById(R.id.editText4);//date
dd11.setText(data123);
Toast.makeText(InvoiceActivity.this,"Date set to: " + data123, Toast.LENGTH_LONG).show();

}

日历 Activity .java:

public class CalendarActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);

//Get the bundle
Bundle bundle222 = getIntent().getExtras();

//Extract the data…
String data1 = bundle222.getString("stuff11");
EditText et1 = (EditText) findViewById(R.id.editText111);
et1.setText(data1);
//et1.setEnabled(false);

String data2 = bundle222.getString("stuff22");
EditText et2 = (EditText) findViewById(R.id.editText222);
et2.setText(data2);
//et2.setEnabled(false);
}

最佳答案

也许举个例子会更清楚。

Intent intent = new Intent(context, YourActivity.class);
intent.putExtra("Name of extra", yourObject); // Your object can be a String, int, Bundle, Parcelable, etc...

在你的 Activity 中

@Override
public void onCreate(Bundle savedInstanceState) {
Bundle bundle = getIntent().getBundleExtra("Name of extra"); // If that was a bundle
YourType type = (YourType)getIntent().getSerializableExtra("Name of extra"); // If that was a serializable object
// Etc...
}

关于java - Android Intent 不工作(将数据发送到 Activity 并接收回来),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38738289/

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