空指針異常: 04-27 01:13:57.270: E/AndroidRuntime(4942): FATAL EXCEPTION: main04-27 01:13:57.270: E/AndroidRuntime(4942): Process: com.itheima.mobilesafe74, ...
空指針異常:
04-27 01:13:57.270: E/AndroidRuntime(4942): FATAL EXCEPTION: main
04-27 01:13:57.270: E/AndroidRuntime(4942): Process: com.itheima.mobilesafe74, PID: 4942
04-27 01:13:57.270: E/AndroidRuntime(4942): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itheima.mobilesafe74/com.itheima.mobilesafe74.activity.SetupOverActivity}: java.lang.NullPointerException
04-27 01:13:57.270: E/AndroidRuntime(4942): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-27 01:13:57.270: E/AndroidRuntime(4942): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-27 01:13:57.270: E/AndroidRuntime(4942): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-27 01:13:57.270: E/AndroidRuntime(4942): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-27 01:13:57.270: E/AndroidRuntime(4942): at android.os.Handler.dispatchMessage(Handler.java:102)
04-27 01:13:57.270: E/AndroidRuntime(4942): at android.os.Looper.loop(Looper.java:136)
04-27 01:13:57.270: E/AndroidRuntime(4942): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-27 01:13:57.270: E/AndroidRuntime(4942): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 01:13:57.270: E/AndroidRuntime(4942): at java.lang.reflect.Method.invoke(Method.java:515)
04-27 01:13:57.270: E/AndroidRuntime(4942): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-27 01:13:57.270: E/AndroidRuntime(4942): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-27 01:13:57.270: E/AndroidRuntime(4942): at dalvik.system.NativeStart.main(Native Method)
04-27 01:13:57.270: E/AndroidRuntime(4942): Caused by: java.lang.NullPointerException
04-27 01:13:57.270: E/AndroidRuntime(4942): at com.itheima.mobilesafe74.activity.SetupOverActivity.initUI(SetupOverActivity.java:46)
04-27 01:13:57.270: E/AndroidRuntime(4942): at com.itheima.mobilesafe74.activity.SetupOverActivity.onCreate(SetupOverActivity.java:38)
04-27 01:13:57.270: E/AndroidRuntime(4942): at android.app.Activity.performCreate(Activity.java:5231)
04-27 01:13:57.270: E/AndroidRuntime(4942): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-27 01:13:57.270: E/AndroidRuntime(4942): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
相應代碼:
1 package com.itheima.mobilesafe74.activity; 2 3 import com.itheima.mobilesafe74.R; 4 import com.itheima.mobilesafe74.utils.ConstentValue; 5 import com.itheima.mobilesafe74.utils.SpUtil; 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.TextView; 12 13 /** 14 * 說明 :手機安全導航設置完成的界面 15 */ 16 public class SetupOverActivity extends Activity 17 { 18 @Override 19 protected void onCreate(Bundle savedInstanceState) 20 { 21 super.onCreate(savedInstanceState); 22 // setContentView(R.layout.activity_setup_over); 23 boolean setup_over = SpUtil.getBoolean(this, ConstentValue.SETUP_OVER, 24 false); 25 if (setup_over) 26 { 27 // 手機安全導航設置完成跳轉到設置完成的界面 28 setContentView(R.layout.activity_setup_over); 29 } else 30 { 31 // 手機安全導航設置沒有完成跳轉到導航設置1的界面 32 Intent intent = new Intent(this, Setup1Activity.class); 33 startActivity(intent); 34 // 跳轉到一個新的界面時需要關閉當前界面 35 finish(); 36 } 37 initUI(); 38 } 39 40 private void initUI() 41 { 42 43 TextView tv_safe_number = (TextView) findViewById(R.id.tv_safe_number); 44 String phone = SpUtil.getString(this, ConstentValue.CONTACT_PHONE, ""); 45 tv_safe_number.setText(phone); 46 // 重新進入設置嚮導textview點擊偵聽 47 TextView tv_reset_setting = (TextView) findViewById(R.id.tv_reset_setting); 48 tv_reset_setting.setOnClickListener(new OnClickListener() 49 { 50 51 @Override 52 public void onClick(View v) 53 { 54 // 手機安全導航設置沒有完成跳轉到導航設置1的界面 55 Intent intent = new Intent(getApplicationContext(), 56 Setup1Activity.class); 57 startActivity(intent); 58 // 跳轉到一個新的界面時需要關閉當前界面 59 finish(); 60 } 61 }); 62 } 63 }View Code
分析:由於調用initUI(),方法之前沒有載入佈局文件setContentView(R.layout.activity_setup_over);
導致後面45行tv_safe_number和48行tv_reset_setting通過findviewbyid方法調用時報出空指針
解決方案1:在protected void onCreate(Bundle savedInstanceState)方法體中添加setContentView(R.layout.activity_setup_over),
解決方案2:在45行tv_safe_number和48行tv_reset_setting用到這兩個指針是做if(tv_safe_number!=null)判斷