从Android中的意图中获取额外内容

时间:2013-01-22 19:37:04

标签: android android-intent bundle extras

我有一个让我发疯的问题,我希望从捆绑中获取“n”字符串值,当我点击“发送按钮”时,不知何故avd崩溃。

活动1

public void enviar(View view){
    Intent intento=new Intent(this, Segunda.class);

    Bundle bundle=new Bundle();
    String usuarioS=editText.getText().toString();
    editText1 = (EditText) findViewById(R.id.usuarioID);        
    bundle.putString("usuario", usuarioS);
    intento.putExtras(bundle);



    startActivity(intento);
}

活动2

public class Segunda extends Activity {

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


    String message,message1;

        Bundle extras = getIntent().getExtras();

            message1 = extras.getString("usuario"); 
            TextView textView1 = (TextView)findViewById(R.id.usuariofID);       
            textView1.setText(message1);



}

这是我的代码。我搞砸了哪里?

logcat的(更新)

01-22 19:54:54.055: W/Trace(1270): Unexpected value from nativeGetEnabledTags: 0
01-22 19:54:54.104: D/AndroidRuntime(1270): Shutting down VM
01-22 19:54:54.104: W/dalvikvm(1270): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-22 19:54:54.155: E/AndroidRuntime(1270): FATAL EXCEPTION: main
01-22 19:54:54.155: E/AndroidRuntime(1270): java.lang.IllegalStateException: Could not execute method of the activity
01-22 19:54:54.155: E/AndroidRuntime(1270):     at android.view.View$1.onClick(View.java:3597)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at android.view.View.performClick(View.java:4202)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at android.view.View$PerformClick.run(View.java:17340)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at android.os.Handler.handleCallback(Handler.java:725)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at android.os.Looper.loop(Looper.java:137)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at java.lang.reflect.Method.invokeNative(Native Method)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at java.lang.reflect.Method.invoke(Method.java:511)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at dalvik.system.NativeStart.main(Native Method)
01-22 19:54:54.155: E/AndroidRuntime(1270): Caused by: java.lang.reflect.InvocationTargetException
01-22 19:54:54.155: E/AndroidRuntime(1270):     at java.lang.reflect.Method.invokeNative(Native Method)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at java.lang.reflect.Method.invoke(Method.java:511)
01-22 19:54:54.155: E/AndroidRuntime(1270):     at android.view.View$1.onClick(View.java:3592)
01-22 19:54:54.155: E/AndroidRuntime(1270):     ... 11 more
01-22 19:54:54.155: E/AndroidRuntime(1270): Caused by: java.lang.NullPointerException
01-22 19:54:54.155: E/AndroidRuntime(1270):atcom.example.actividadesss.MainActivity.enviar(MainActivity.java:301-22 19:54:54.155: E/AndroidRuntime(1270):   ... 14 more
01-22 19:54:54.224:W/ActivityManager(286):Forcefinishingactivitycom.example.actividadesss/.MainActivity

2 个答案:

答案 0 :(得分:1)

似乎您在建立它之前使用了EditText字段。你能试试......

public void enviar(View view){
   ...
   String usuarioS=editText.getText().toString();
   editText1 = (EditText) findViewById(R.id.usuarioID);
   ...
}

到:

public void enviar(View view){
   ...
   editText = (EditText) findViewById(R.id.usuarioID);
   String usuarioS=editText.getText().toString();
   ...
}

答案 1 :(得分:1)

首先,我建议删除不需要的代码。您可以使用Intent.putExtra()而不是创建一个bundle并将bundle放入Intent中。最小化代码可以帮助调试您的问题,有时甚至可以解决它。

我同意David M在使用它之前初始化你的EditText。

public void enviar(View view){
    Intent intento=new Intent(this, Segunda.class);

    editText = (EditText) findViewById(R.id.usuarioID);
    String usuarioS=editText.getText().toString();
    intento.putExtra("usuario", usuarios);

    startActivity(intento);
}

Understanding Intents