[英]Null Pointer Exception by using Intent putExtra
i trying to use an Intent to put a Integer value to an new activity. 我试图使用一个意图将整数值添加到一个新的活动。 I tryed many solutions based by this forum but nothing is working for me. 我尝试了该论坛提供的许多解决方案,但没有任何帮助。
If i call the activity without intent.putExtra, the new activity start without any problems. 如果我在没有intent.putExtra的情况下调用活动,则新的活动将开始而不会出现任何问题。
I get always the fallowing Error: 我总是得到轻松的错误:
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>
Here is my main activity: 这是我的主要活动:
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");}}
Here is the code of the target activity: 这是目标活动的代码:
@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);
The issue was with invoking 问题在于调用
tw.setText(LacStr)
since findViewById failed to find textView
and returned null. tw.setText(LacStr)
因为findViewById未能找到textView
和返回null。 Thus a null pointer exception occurred. 因此,发生了空指针异常。
To help in the future, looking at the line-number in the stacktrace (27) can help isolate the issue. 为了将来提供帮助,查看stacktrace(27)中的行号可以帮助找出问题所在。 Alternatively, using debug mode can check if a function succeeds (returns true) or fails (returns false). 或者,使用调试模式可以检查函数是否成功(返回true)或失败(返回false)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.