如何使用bundle发送超过1个EditText值

时间:2013-05-01 08:43:05

标签: android android-intent bundle back

我制作了一个简单的android程序,有两个活动,第一个活动包含一些edittexts和一个按钮,第二个活动包含相同数量的textviews。现在,当第一个活动中的按钮按下Edittexts中的文本应该转到第二个activity的textViews.I尝试过如下代码,但它不起作用:

MainActivity.java

package com.example.myweb;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
     public  String EXTRA_MESSAGE = "com.example.myweb";
     Button b;
     EditText ed1,ed2,ed3,ed4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


         b =(Button)findViewById(R.id.send);
         ed1 = (EditText)findViewById(R.id.edit_msg);
         ed2=(EditText)findViewById(R.id.edit_msg1);
         ed3=(EditText)findViewById(R.id.edit_msg3);
         ed4=(EditText)findViewById(R.id.edit_msg4);


                 b.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                         // TODO Auto-generated method stub


                         Intent i = new Intent(MainActivity.this,Act2.class);
                     Bundle b=new Bundle();
                     String fname=ed1.getText().toString();
                     String lname=ed2.getText().toString();
                     String fon=ed3.getText().toString();
                     String city= ed4.getText().toString();

                     b.putString("Extra__fname", fname);
                     b.putString("Extra__lname", lname);
                     b.putString("Extra__fon", fon);
                     b.putString("Extra__city", city);

                     i.putExtras(b);
                     startActivity(i);



                     /*i.putExtra("EXTRA_MESSAGE", s);
                         startActivity(i);*/

                     }
                 });

    }

}

Act2.java

package com.example.myweb;

import org.w3c.dom.Text;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Act2 extends Activity {

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

         Intent i = getIntent();

         Bundle b =i.getExtras();
         String fname=i.getStringExtra("fname");
         String lname=i.getStringExtra("lname");
         String fon=i.getStringExtra("fon");
         String city=i.getStringExtra("city");



        // String text = i.getStringExtra("EXTRA_MESSAGE");

            TextView t = (TextView)findViewById(R.id.tv1);
            TextView t1 = (TextView)findViewById(R.id.tv2);
            TextView t2 = (TextView)findViewById(R.id.tv3);
            TextView t3 = (TextView)findViewById(R.id.tv4);

            t.setText(fname);
            t1.setText(lname);
            t2.setText(fon);
            t3.setText(city);



    }
}

logcat的

05-10 15:09:07.877: W/KeyCharacterMap(1072): No keyboard for id 0
05-10 15:09:07.877: W/KeyCharacterMap(1072): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-10 15:09:16.976: D/AndroidRuntime(1072): Shutting down VM
05-10 15:09:16.976: W/dalvikvm(1072): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-10 15:09:16.998: E/AndroidRuntime(1072): FATAL EXCEPTION: main
05-10 15:09:16.998: E/AndroidRuntime(1072): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listdemo/com.example.listdemo.SecondActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-10 15:09:16.998: E/AndroidRuntime(1072):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at android.os.Looper.loop(Looper.java:123)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at java.lang.reflect.Method.invokeNative(Native Method)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at java.lang.reflect.Method.invoke(Method.java:507)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at dalvik.system.NativeStart.main(Native Method)
05-10 15:09:16.998: E/AndroidRuntime(1072): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-10 15:09:16.998: E/AndroidRuntime(1072):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at android.app.Activity.setContentView(Activity.java:1657)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at com.example.listdemo.SecondActivity.onCreate(SecondActivity.java:22)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-10 15:09:16.998: E/AndroidRuntime(1072):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-10 15:09:16.998: E/AndroidRuntime(1072):     ... 11 more

2 个答案:

答案 0 :(得分:1)

强文尝试:

     Bundle b =i.getExtras();
     String fname=b.getStringExtra("fname");
     String lname=b.getStringExtra("lname");
     String fon=b.getStringExtra("fon");
     String city=b.getStringExtra("city");

这也有效:

Intent i = new Intent(MainActivity.this,Act2.class);
i.putExtra("Extra__fname", fname);
i.putExtra("Extra__lname", lname);
i.putExtra("Extra__fon", fon);
i.putExtra("Extra__city", city);
startActivity(i);

然后在您的activity2中:

    Bundle bundle = getIntent().getExtras();
    if(bundle != null)
    {
      fname= bundle.getString("Extra__fname");
      lname= bundle.getString("Extra__lname");
      fon= bundle.getString("Extra__fon");
      cit= bundle.getString("Extra__city");
    }

答案 1 :(得分:1)

您在发送值和检索值时使用不同的名称。

b.putString("Extra__fname", fname);
b.putString("Extra__lname", lname);
b.putString("Extra__fon", fon);
b.putString("Extra__city", city);

您正在使用Extra__fname发送fname并使用fname检索它。 在其他情况下也类似。

String fname=i.getStringExtra("fname");
String lname=i.getStringExtra("lname");
String fon=i.getStringExtra("fon");
String city=i.getStringExtra("city");

在两个地方保持名称相同。