Android Intent无法正常工作(将数据发送到活动并接收回来)

时间:2016-08-03 08:26:23

标签: java android android-intent

我想将两个EditText数据发送到下一个活动,并在点击按钮时将其恢复。 但每当我通过意图发送数据时,我的活动就会崩溃。我从昨天起就被困住了......

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();

}

CalendarActivity.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);
}

1 个答案:

答案 0 :(得分:1)

也许有一个更清晰的例子。

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

在YourActivity中

@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...
}