我的应用程序工作正常,除非我旋转设备,在这种情况下,它提供NullPointerException
。要处理屏幕旋转,我已经覆盖了onSaveInstanceState
。我检查了logcat,它也说Caused by: java.lang.NullPointerException
10-28 23:51:20.769: E/AndroidRuntime(25185): at com.bignerdranch.android.geoquiz.CheatActivity.onCreate(CheatActivity.java:42)
第42行的代码为mAnswerTextView.setText(R.string.false_button)
。 java文件以及logcat详细信息发布在下面。可能是什么原因?我是新手。
的java:
public class CheatActivity extends Activity {
public static final String EXTRA_ANSWER_IS_TRUE =
"com.bignerdranch.android.geoquiz.answer_is_true";
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
public static final String EXTRA_ANSWER_SHOWN="om.bignerdranch.android.geoquiz.answer_shown";
public static final String KEY_CHEATER = "cheater";
private boolean mAnswerShown;
private void setAnswerShowResult(boolean isAnswerShown){
Intent data= new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK,data);
mAnswerShown = isAnswerShown;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activiy_cheat);
if (savedInstanceState == null) {
setAnswerShowResult(false);
}
if (savedInstanceState != null){
setAnswerShowResult(savedInstanceState.getBoolean(KEY_CHEATER, false));
if(mAnswerIsTrue){
mAnswerTextView.setText(R.string.true_button);
} else{
mAnswerTextView.setText(R.string.false_button);
}
}
mAnswerIsTrue = getIntent()
.getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);
mAnswerTextView= (TextView) findViewById(R.id.answerTextView);
mShowAnswer=(Button) findViewById(R.id.showAnswerButton);
//setAnswerShowResult(false);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mAnswerIsTrue){
mAnswerTextView.setText(R.string.true_button);
}else{
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShowResult(true);
}
});
}
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean(KEY_CHEATER, mAnswerShown);
}
}
logcat的:
10-28 23:51:20.769: E/AndroidRuntime(25185): FATAL EXCEPTION: main
10-28 23:51:20.769: E/AndroidRuntime(25185): Process: com.bignerdranch.android.geoquiz, PID: 25185
10-28 23:51:20.769: E/AndroidRuntime(25185): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.geoquiz/com.bignerdranch.android.geoquiz.CheatActivity}: java.lang.NullPointerException
10-28 23:51:20.769: E/AndroidRuntime(25185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
10-28 23:51:20.769: E/AndroidRuntime(25185): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
10-28 23:51:20.769: E/AndroidRuntime(25185): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3805)
10-28 23:51:20.769: E/AndroidRuntime(25185): at android.app.ActivityThread.access$900(ActivityThread.java:139)
10-28 23:51:20.769: E/AndroidRuntime(25185): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1216)
10-28 23:51:20.769: E/AndroidRuntime(25185): at android.os.Handler.dispatchMessage(Handler.java:102)
10-28 23:51:20.769: E/AndroidRuntime(25185): at android.os.Looper.loop(Looper.java:136)
10-28 23:51:20.769: E/AndroidRuntime(25185): at android.app.ActivityThread.main(ActivityThread.java:5086)
10-28 23:51:20.769: E/AndroidRuntime(25185): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 23:51:20.769: E/AndroidRuntime(25185): at java.lang.reflect.Method.invoke(Method.java:515)
10-28 23:51:20.769: E/AndroidRuntime(25185): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-28 23:51:20.769: E/AndroidRuntime(25185): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-28 23:51:20.769: E/AndroidRuntime(25185): at dalvik.system.NativeStart.main(Native Method)
10-28 23:51:20.769: E/AndroidRuntime(25185): Caused by: java.lang.NullPointerException
10-28 23:51:20.769: E/AndroidRuntime(25185): at com.bignerdranch.android.geoquiz.CheatActivity.onCreate(CheatActivity.java:42)
10-28 23:51:20.769: E/AndroidRuntime(25185): at android.app.Activity.performCreate(Activity.java:5248)
10-28 23:51:20.769: E/AndroidRuntime(25185): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
10-28 23:51:20.769: E/AndroidRuntime(25185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
10-28 23:51:20.769: E/AndroidRuntime(25185): ... 12 more
答案 0 :(得分:2)
在为setText()
分配值之前,您在mAnswerTextView
上呼叫mAnswerTextView
。在致电savedInstanceState
以检索小部件后,将onCreate()
工作移至findViewById()
的末尾。
答案 1 :(得分:2)
在第42行,您正在setText
上尝试拨打mAnswerTextView
,但尚未初始化。 (因为当您更改设备的方向时,会再次调用onCreate
)。
要解决此问题,您只需在致电TextView
之前初始化setText
。移动以下行
mAnswerTextView= (TextView) findViewById(R.id.answerTextView);
上面检查savedInstanceState
是否为空(只要它仍然在setContentView
之后,在此之前调用它会导致另一个错误)
if (savedInstanceState != null){
....
这将确保首次创建活动时,以及重新创建活动时,您都可以在setText
TextView