android 中的空指针异常未启动 activity

null pointer exception in android not starting activity

这是我的代码 MainActivity。无法找出我哪里出错了,因为我是 android 的新手,我们将不胜感激。在共享首选项中保存值,但是当我 运行 这会抛出异常,如下 logcat

中所述
public class MainActivity extends Activity implements View.OnClickListener {

CheckBox checkBox;
EditText editText;
Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    checkBox = (CheckBox) findViewById(R.id.checkbox);
    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
    loadSavedPreferences();
}





private void loadSavedPreferences() {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    boolean checkBoxValue = sharedPreferences.getBoolean("CheckBox_Value", false);
    String name = sharedPreferences.getString("storedName", "YourName");
    if (checkBoxValue) {
        checkBox.setChecked(true);
    } else {
        checkBox.setChecked(false);
    }
    editText.setText(name);
}

private void savePreferences(String key, boolean value) {

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}

private void savePreferences(String key, String value) {

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

@Override
public void onClick(View v) {
    savePreferences("CheckBox_Value", checkBox.isChecked());
    if (checkBox.isChecked())
    savePreferences("storedName", editText.getText().toString());
    finish();

}

}

这是logcat

03-20 21:38:28.420    4574-4574/com.example22.dell.sharedprefences E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example22.dell.sharedprefences, PID: 4574
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example22.dell.sharedprefences/com.example22.dell.sharedprefences.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example22.dell.sharedprefences.MainActivity.onCreate(Unknown Source)
            at android.app.Activity.performCreate(Activity.java:5264)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)

布局xml文件

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_marginTop="89dp"
    android:layout_below="@+id/editText"
    android:layout_centerHorizontal="true" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Are You Sure ?"
    android:id="@+id/checkBox"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false" />

您在 xml 中的复选框 ID 在您使用 "checkbox" 的代码中是 "checkBox"。

我不知道你的构建工具,但在 R.java 文件中,常量 R.id.checkbox 不应该存在,除非你重构它并且 R.java 没有更新。