0

如果我使用正确的登录名,它可以正常工作,但是当我尝试使用虚假登录详细信息对其进行测试时,它不会显示错误消息。它只是崩溃了。

这是代码

       public void onClick(View view) {
           String email = inputEmail.getText().toString();
           String password = inputPassword.getText().toString();
           UserFunctions userFunction = new UserFunctions();
           JSONObject json = userFunction.loginUser(email, password);

            //check for login response
           try {
               if (json.getString(KEY_SUCCESS) != null) {
                   loginErrorMsg.setText("");
                   String res = json.getString(KEY_SUCCESS);
                   if(Integer.parseInt(res) == 1){
                        //user successfully logged in
                        //Store user details in SQLite Database
                       DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                       JSONObject json_user = json.getJSONObject("user");

                       // Clear all previous data in database
                       userFunction.logoutUser(getApplicationContext());
                       db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                        

                        //Launch Dashboard Screen
                       Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

                        //Close all views before launching Dashboard
                       dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                       startActivity(dashboard);

                        //Close Login Screen
                       finish();
                   }else{
                        //Error in login
                       loginErrorMsg.setText("Incorrect username/password");
                   }
               }
           } catch (JSONException e) {
               e.printStackTrace();
           }
       }
   });

这是日志猫

FATAL EXCEPTION: main
 java.lang.NullPointerException
    at your.dissertation.project.LoginActivity$1.onClick(LoginActivity.java:62)
    at android.view.View.performClick(View.java:2533)
    at android.view.View$PerformClick.run(View.java:9320)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:4385)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

检查以确保您json的对象在拨打电话后不为空JSONObject json = userFunction.loginUser(email, password);

于 2013-04-29T20:27:32.703 回答