0

我在开始我的活动时收到 NullPointerException 错误。我的 logcat 说它发生在我的活动中,但没有指定在哪里。我第一次尝试使用 SharedPreferences 并且不知道错误发生在哪里。

这是我的活动:

    public class SharedPrefLoginActivity extends Activity {


private Button cancelBttn;
private EditText rname;
private EditText rpwrd;
private Button rBttn;
/** Called when the activity is first created. */

private SharedPreferences dhj;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dhj = this.getSharedPreferences("DHJ", MODE_WORLD_READABLE);
    if(dhj.getString("password", null) !=null) {
    setContentView(R.layout.register);
    rname = (EditText) findViewById(R.id.reg_uname);
    rpwrd = (EditText) findViewById(R.id.reg_pswd);
    rBttn = (Button) findViewById(R.id.reg_button);

    rBttn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            SharedPreferences.Editor dhjEditor = dhj.edit();
            dhjEditor.putString("rname", rname.getText().toString());
            dhjEditor.putString("rpwrd", rpwrd.getText().toString());
            dhjEditor.commit();
            Toast.makeText(getApplicationContext(), "Credentials Saved!", Toast.LENGTH_SHORT).show();
    }
     }
      );
       }
    else { 

        Intent i = new Intent(SharedPrefLoginActivity.this, AccessApp.class);
        startActivity(i);   
    }
    cancelBttn.setOnClickListener (new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Close application
            finish();
    }
    });     
    }}

日志猫:

    09-14 09:27:26.244: E/AndroidRuntime(589): Uncaught handler: thread main exiting  due to uncaught exception
    09-14 09:27:26.293: E/AndroidRuntime(589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.SharedPreferences.Login/com.SharedPreferences.Login.SharedPrefLoginActivity}: java.lang.NullPointerException
    09-14 09:27:26.293: E/AndroidRuntime(589):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at android.os.Handler.dispatchMessage(Handler.java:99)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at android.os.Looper.loop(Looper.java:123)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at android.app.ActivityThread.main(ActivityThread.java:4363)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at java.lang.reflect.Method.invokeNative(Native Method)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at java.lang.reflect.Method.invoke(Method.java:521)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at dalvik.system.NativeStart.main(Native Method)
    09-14 09:27:26.293: E/AndroidRuntime(589): Caused by: java.lang.NullPointerException
    09-14 09:27:26.293: E/AndroidRuntime(589):  at com.SharedPreferences.Login.SharedPrefLoginActivity.onCreate(SharedPrefLoginActivity.java:58)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    09-14 09:27:26.293: E/AndroidRuntime(589):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
4

3 回答 3

3

您忘记设置cancelBttn字段:

 cancelBttn = (Button) findViewById(R.id.cancel_button); // Insert your R.id.XXX here.

也搬

 setContentView(R.layout.register);

就在之后super.onCreate(savedInstanceState);。发生这种NullPointerException情况是因为布局没有膨胀。您可以在堆栈跟踪的最后 4 行中看到这一点。

于 2012-09-15T18:00:25.353 回答
2

可能是这条线导致它:

cancelBttn.setOnClickListener (new OnClickListener()

我没有看到任何实例化,cancelBttn所以它在这条线上是空的......

于 2012-09-15T18:02:23.907 回答
0

这些行

Caused by: java.lang.NullPointerException
09-14 09:27:26.293: E/AndroidRuntime(589):  at     com.SharedPreferences.Login.SharedPrefLoginActivity.onCreate(SharedPrefLoginActivity.java:58)
09-14 09:27:26.293: E/AndroidRuntime(589):  at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

让您知道您的 NullPointer 来自 onCreate 方法。在注册它的侦听器之前,您必须首先实例化您的 cancelButtn 变量。我认为这不是 dhj 成员的问题,因为它没有附加到视图,所以布局不需要在被引用之前被膨胀..尽管在调用后设置它的值是明智的设置内容视图()。

于 2012-09-16T16:45:14.747 回答