NullPointerException NULL pointer exception -- no layout file is loaded to acitivy beforehand -- missing: setContentView (R. layout. activity_setup_over );,
Null Pointer exception:
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. initialize mlaunchactivity (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. Wait mcreate (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. initialize mlaunchactivity (ActivityThread. java: 2159)
Code:
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 * Description: Ui 15 */16 public class SetupOverActivity extends Activity17 {18 @ Override19 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 // go to the configured page 28 setContentView (R. layout. activity_setup_over); 29} else30 {31 // the mobile phone security navigation settings are not completed. Jump to the page of navigation settings 1. 32 Intent intent Intent = new Intent (this, Setup1Activity. class); 33 startActivity (intent); 34 // when you jump to a new interface, you need to close the current interface 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 // re-enter the settings wizard textview and click listen 47 TextView TV _reset_setting = (TextView) findViewById (R. id. TV _reset_setting); 48 TV _reset_setting.setOnClickListener (new OnClickListener () 49 {50 51 @ Override52 public void onClick (View v) 53 {54 // the mobile phone security navigation settings are not completed. Jump to the page of navigation settings 1 55 Intent intent = new Intent (getApplicationContext (), 56 Setup1Activity. class); 57 startActivity (intent); 58 // when you jump to a new interface, you need to close the current interface 59 finish (); 60} 61}); 62} 63}
View Code
Analysis: Because initUI () is called, the layout file setContentView (R. layout. activity_setup_over) is not loaded before the method );
As a result, the next 45 rows of TV _safe_number and 48 rows of TV _reset_setting call the findviewbyid method to return a null pointer.
Solution 1: Add setContentView (R. layout. activity_setup_over) to the protected void onCreate (Bundle savedInstanceState) method body ),
Solution 2: In 45 rows of TV _safe_number and 48 rows of TV _reset_setting, these two pointers are used for if (TV _safe_number! = Null) Judgment