1
我試圖使用一個意圖把一個整數值放到一個新的活動。我嘗試了許多基於這個論壇的解決方案,但沒有任何解決方案適用於我。通過使用意圖的空指針異常putExtra
如果我在沒有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);
如果您只是在putExtra中出現錯誤,並不一定意味着這就是您獲得空指針異常的原因。這是27線嗎? –
第27行的目標活動是tw.setText(LacStr); – Syntoo
堆棧跟蹤看起來像NPE在onCreate中。如果我必須打賭,因爲沒有找到R.id.textView。 –