使用简单的Intent Extra时出现Java空指针异常

时间:2013-12-09 19:08:48

标签: java android android-intent

当我尝试将一些文本发送到下一个Activity时,程序崩溃说“空指针异常无法启动活动”,但未使用Intent Extra Intent正常工作。< / p>

主要活动

public class MainActivity extends Activity implements OnClickListener{
EditText et;

public final static String KEY="com.example.myprotrial";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et = (EditText) findViewById(R.id.editText1);
    Button btn =(Button)findViewById(R.id.button1);
    btn.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View arg0) {
    Intent i = new Intent(MainActivity.this,MyActivity.class);
    i.putExtra(KEY, et.getText().toString());
    startActivity(i);
}

}

MyActivity

public class MyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Intent intent=getIntent();
    String a = intent.getStringExtra(MainActivity.KEY);
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(a);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}
}

logcat的

12-09 13:58:33.115: E/AndroidRuntime(1588): FATAL EXCEPTION: main
12-09 13:58:33.115: E/AndroidRuntime(1588): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myprotrial/com.example.myprotrial.MyActivity}: java.lang.NullPointerException
12-09 13:58:33.115: E/AndroidRuntime(1588):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at android.os.Looper.loop(Looper.java:137)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at java.lang.reflect.Method.invoke(Method.java:525)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at dalvik.system.NativeStart.main(Native Method)
12-09 13:58:33.115: E/AndroidRuntime(1588): Caused by: java.lang.NullPointerException
12-09 13:58:33.115: E/AndroidRuntime(1588):     at com.example.myprotrial.MyActivity.onCreate(MyActivity.java:18)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at android.app.Activity.performCreate(Activity.java:5133)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 13:58:33.115: E/AndroidRuntime(1588):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-09 13:58:33.115: E/AndroidRuntime(1588):     ... 11 more

1 个答案:

答案 0 :(得分:1)

问题是你的LogCat没有告诉NPE在哪一行发生。

从你的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Intent intent=getIntent();
    String a = intent.getStringExtra(MainActivity.KEY);
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(a);
}

如果NPE在intent.getStringExtra上发生,则您的意图为空。如果它位于tv.setText(a),那么activity_my布局很可能没有textView1。在调试器中逐步执行它并观察实例变量。