0

我有一个让我发疯的问题,我想从包中获取“n”字符串值,当我单击“发送按钮”时,avd 会以某种方式崩溃。

活动一

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

活动二

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
4

2 回答 2

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();
   ...
}
于 2013-01-22T20:08:19.510 回答
1

首先,我建议摆脱不需要的代码。您可以使用 Intent.putExtra() 而不是制作捆绑包并将捆绑包放入 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);
}

了解意图

于 2013-01-22T20:18:26.327 回答