NullPointerException错误。我似乎无法发现这个问题

时间:2015-02-14 13:24:32

标签: java android

Logcat错误消息。

当我尝试在应用程序上注册帐户时,我收到此错误,应用程序暂时崩溃并返回登录页面。

 02-14 13:11:19.857  31983-31983/example.com.androidim E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: example.com.androidim, PID: 31983
    java.lang.NullPointerException
            at example.com.androidim.SignUp$2$1$1.run(SignUp.java:126)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            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:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606     
            at dalvik.system.NativeStart.main(Native Method)

以下是SignUp活动中的第111-142行。

 if (passwordText.getText().toString().equals(passwordAgainText.getText().toString())){

                        if (usernameText.length() >= 5 && passwordText.length() >= 5) {

                            Thread thread = new Thread(){
                                String result = new String();
                                @Override
                                public void run() {
                                    result = imService.signUpUser(usernameText.getText().toString(),
                                            passwordText.getText().toString(),
                                            eMailText.getText().toString());

 handler.post(new Runnable(){

    public void run() {
        if (result.equals(SERVER_RES_RES_SIGN_UP_SUCCESFULL)) {
            Toast.makeText(getApplicationContext(),R.string.signup_successfull, Toast.LENGTH_LONG).show();
            showDialog(SIGN_UP_SUCCESSFULL);
        }
        else if (result.equals(SERVER_RES_SIGN_UP_USERNAME_CRASHED)){
            Toast.makeText(getApplicationContext(),R.string.signup_username_crashed, Toast.LENGTH_LONG).show();
            showDialog(SIGN_UP_USERNAME_CRASHED);
        }
        else  //if (result.equals(SERVER_RES_SIGN_UP_FAILED))
        {
            Toast.makeText(getApplicationContext(),R.string.signup_failed, Toast.LENGTH_LONG).show();
            showDialog(SIGN_UP_FAILED);
        }
    }

});

}

1 个答案:

答案 0 :(得分:0)

equals变量上调用result时,resultnull。在调用equals之前,您必须进行空检查。

public void run() {
    if(result != null){
        if (result.equals(SERVER_RES_RES_SIGN_UP_SUCCESFULL)) {
            Toast.makeText(getApplicationContext(),R.string.signup_successfull, Toast.LENGTH_LONG).show();
            showDialog(SIGN_UP_SUCCESSFULL);
        }
        else if (result.equals(SERVER_RES_SIGN_UP_USERNAME_CRASHED)){
            Toast.makeText(getApplicationContext(),R.string.signup_username_crashed,       Toast.LENGTH_LONG).show();
            showDialog(SIGN_UP_USERNAME_CRASHED);
        }
        else  //if (result.equals(SERVER_RES_SIGN_UP_FAILED))
        {
            Toast.makeText(getApplicationContext(),R.string.signup_failed, Toast.LENGTH_LONG).show();
            showDialog(SIGN_UP_FAILED);
        }else{
            //result == null
        }
    }
}