使用Intent putExtra的空指针异常

时间:2015-09-03 23:52:29

标签: java android android-intent nullpointerexception

我尝试使用Intent将Integer值放入新活动。我尝试了很多基于这个论坛的解决方案,但没有什么对我有用。

如果我在没有intent.putExtra的情况下调用活动,则新活动开始没有任何问题。

我总是得到一个错误的错误:

09-03 19:08:24.665  13259-13259/com.example.test_bga E/AndroidRuntime﹕ 
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.test_bga/com.example.test_bga.BGA_resp_alk}: 
java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.test_bga.BGA_resp_alk.onCreate(BGA_resp_alk.java:27)
        at android.app.Activity.performCreate(Activity.java:5133)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)</code>

这是我的主要活动:

public void analyse(){
werteSumme = phSumme + pco2Summe + hco3Summe + beSumme;
[...]
else if (werteSumme == 3122){
    Intent intent = new Intent(Blutgasanalyse.this,BGA_resp_alk.class);
    intent.putExtra("LactatExtra", lacSumme.intValue());
    Blutgasanalyse.this.startActivity(intent);
}
[...]
// works perfectly: (so the lacSumme is not null?!)
if (lacSumme == 0){twDebug2.setText("Kein Wert (0)");}
else {
   if (lacSumme == 1){twDebug2.setText("Lactat erniedrigt");}
   else if (lacSumme == 2)     {twDebug2.setText("Lactat Normal");}
   else if (lacSumme == 3)     {twDebug2.setText("Lactat erhöht");}}

以下是目标活动的代码:

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

 tw = (TextView) findViewById(R.id.textView);
 Intent intent = getIntent();
 LacInt = intent.getIntExtra("LactatExtra", 456);
 LacStr = LacInt.toString();
 tw.setText(LacStr);

1 个答案:

答案 0 :(得分:1)

问题在于调用

tw.setText(LacStr)因为findViewById找不到textView并返回null。因此发生了空指针异常。

为了将来的帮助,查看堆栈跟踪(27)中的行号有助于隔离问题。或者,使用调试模式可以检查函数是否成功(返回true)或失败(返回false)。