空指针异常:
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)
相应代码:
package com.itheima.mobilesafe74.activity; import com.itheima.mobilesafe74.R;
import com.itheima.mobilesafe74.utils.ConstentValue;
import com.itheima.mobilesafe74.utils.SpUtil;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView; /**
* 说明 :手机安全导航设置完成的界面
*/
public class SetupOverActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_setup_over);
boolean setup_over = SpUtil.getBoolean(this, ConstentValue.SETUP_OVER,
false);
if (setup_over)
{
// 手机安全导航设置完成跳转到设置完成的界面
setContentView(R.layout.activity_setup_over);
} else
{
// 手机安全导航设置没有完成跳转到导航设置1的界面
Intent intent = new Intent(this, Setup1Activity.class);
startActivity(intent);
// 跳转到一个新的界面时需要关闭当前界面
finish();
}
initUI();
} private void initUI()
{ TextView tv_safe_number = (TextView) findViewById(R.id.tv_safe_number);
String phone = SpUtil.getString(this, ConstentValue.CONTACT_PHONE, "");
tv_safe_number.setText(phone);
// 重新进入设置向导textview点击侦听
TextView tv_reset_setting = (TextView) findViewById(R.id.tv_reset_setting);
tv_reset_setting.setOnClickListener(new OnClickListener()
{ @Override
public void onClick(View v)
{
// 手机安全导航设置没有完成跳转到导航设置1的界面
Intent intent = new Intent(getApplicationContext(),
Setup1Activity.class);
startActivity(intent);
// 跳转到一个新的界面时需要关闭当前界面
finish();
}
});
}
}
分析:由于调用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)判断