您好我在第493行获得了一个nullpointer,标记在下面的代码中。 有人可以协助解决这个问题吗?我几乎知道100%是复选框(nieuwbel)。 但我无法摆脱异常。
提前谢谢。
@Override
protected void onPause() {
MyBeltegoed dialog = new MyBeltegoed (this, new OnReadyListenerBeltegoed());
nieuwbel = (CheckBox)dialog.findViewById(R.id.nieuwbel);
Editor e = mPrefs.edit();
e.putBoolean(PREF_BOOL, nieuwbel.isChecked()); <----- Nullpointer
e.commit();
Toast.makeText(this, "Settings Saved.", Toast.LENGTH_SHORT).show();
super.onPause();
}
答案 0 :(得分:3)
尝试获取后,您需要检查nieuwbel
是否为null
。 findViewById()
可能找不到您要找的内容:
返回层次结构中具有给定ID的视图或null
试试这个:
@Override
protected void onPause() {
MyBeltegoed dialog = new MyBeltegoed (this, new OnReadyListenerBeltegoed());
nieuwbel = (CheckBox)dialog.findViewById(R.id.nieuwbel);
if (nieuwbel != null) {
Editor e = mPrefs.edit();
e.putBoolean(PREF_BOOL, nieuwbel.isChecked());
e.commit();
Toast.makeText(this, "Settings Saved.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Failed to save settings!", Toast.LENGTH_SHORT).show();
}
super.onPause();
}
Logcat forceclose:
10-31 22:57:36.618: E/AndroidRuntime(1423): FATAL EXCEPTION: main
10-31 22:57:36.618: E/AndroidRuntime(1423): android.app.SuperNotCalledException: Activity {com.sencide/com.sencide.AndroidLogin} did not call through to super.onPause()
10-31 22:57:36.618: E/AndroidRuntime(1423): at android.app.Activity.performPause(Activity.java:3854)
10-31 22:57:36.618: E/AndroidRuntime(1423): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
10-31 22:57:36.618: E/AndroidRuntime(1423): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2341)
10-31 22:57:36.618: E/AndroidRuntime(1423): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311)
10-31 22:57:36.618: E/AndroidRuntime(1423): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2291)
10-31 22:57:36.618: E/AndroidRuntime(1423): at android.app.ActivityThread.access$1700(ActivityThread.java:117)
10-31 22:57:36.618: E/AndroidRuntime(1423): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
10-31 22:57:36.618: E/AndroidRuntime(1423): at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 22:57:36.618: E/AndroidRuntime(1423): at android.os.Looper.loop(Looper.java:123)
10-31 22:57:36.618: E/AndroidRuntime(1423): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-31 22:57:36.618: E/AndroidRuntime(1423): at java.lang.reflect.Method.invokeNative(Native Method)
10-31 22:57:36.618: E/AndroidRuntime(1423): at java.lang.reflect.Method.invoke(Method.java:507)
10-31 22:57:36.618: E/AndroidRuntime(1423): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-31 22:57:36.618: E/AndroidRuntime(1423): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-31 22:57:36.618: E/AndroidRuntime(1423): at dalvik.system.NativeStart.main(Native Method)
答案 1 :(得分:1)
您是否正确设置了内容视图?确保您使用的是创建的R类,而不是默认的android R类。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
编辑:
您还需要为对话框设置内容视图。
@Override
protected void onPause() {
MyBeltegoed dialog = new MyBeltegoed (this, new OnReadyListenerBeltegoed());
dialog.setContentView(R.layout.custom_dialog); <-- add this
nieuwbel = (CheckBox)dialog.findViewById(R.id.nieuwbel);
Editor e = mPrefs.edit();
e.putBoolean(PREF_BOOL, nieuwbel.isChecked());
e.commit();
Toast.makeText(this, "Settings Saved.", Toast.LENGTH_SHORT).show();
super.onPause();
}
custom_dialog
是包含nieuwbel的对话框。 (来自http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog)