如何修复:我尝试登录时自动停止应用程序

时间:2017-05-23 03:38:33

标签: android nullpointerexception android-volley

美好的一天。我需要帮助来修复此错误。当我尝试使用空的用户名和密码登录时,应用程序将不会粉碎或停止。但是当我输入用户名和密码时,应用程序会崩溃或停止。

LoginActivity.java

btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            String username = inputUsername.getText().toString().trim();
            String password = inputPassword.getText().toString().trim();

            // Check for empty data in the form
            if (!username.isEmpty() && !password.isEmpty()) {
                // login user
                checkLogin(username, password);
            } else {
                // Prompt user to enter credentials
                Toast.makeText(getApplicationContext(),
                        "Please enter the credentials!", Toast.LENGTH_LONG)
                        .show();
            }
        }

    });

private void checkLogin(final String username, final String password) {
    // Tag used to cancel the request
    String tag_string_req = "req_login";

    pDialog.setMessage("Logging in ...");
    showDialog();

    StringRequest strReq = new StringRequest(Method.POST,
            AppConfig.URL_LOGIN, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Login Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                // Check for error node in json
                if (!error) {
                    // user successfully logged in
                    // Create login session
                    session.setLogin(true);

                    // Now store the user in SQLite
                    //String uid = jObj.getString("uid");

                    JSONObject user = jObj.getJSONObject("user");
                    String br_code = user.getString("br_code");
                    String mem_id = user.getString("mem_id");
                    String username = user.getString("username");
                    String email = user.getString("email");
                    String created_at = user.getString("created_at");

                    // Inserting row in users table
                    db.addUser(br_code, mem_id, username, email, created_at);

                    // Launch main activity
                    Intent intent = new Intent(LoginActivity.this,
                            MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    // Error in login. Get the error message
                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                // JSON error
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }

logcat的

05-23 12:16:28.740 16910-16910/? D/dalvikvm: Late-enabling CheckJNI
05-23 12:16:28.930 16910-16910/ph.coredev.johnjessbayutas.mobileportal E/Trace: error opening trace file: No such file or directory (2)
05-23 12:16:29.290 16910-16911/ph.coredev.johnjessbayutas.mobileportal D/dalvikvm: GC_CONCURRENT freed 110K, 7% free 6339K/6791K, paused 15ms+4ms, total 44ms
05-23 12:16:29.310 16910-16910/ph.coredev.johnjessbayutas.mobileportal D/libEGL: loaded /system/lib/egl/libEGL_VIVANTE.so
05-23 12:16:29.320 16910-16910/ph.coredev.johnjessbayutas.mobileportal D/libEGL: loaded /system/lib/egl/libGLESv1_CM_VIVANTE.so
05-23 12:16:29.340 16910-16910/ph.coredev.johnjessbayutas.mobileportal D/libEGL: loaded /system/lib/egl/libGLESv2_VIVANTE.so
05-23 12:16:29.420 16910-16910/ph.coredev.johnjessbayutas.mobileportal D/OpenGLRenderer: Enabling debug mode 0
05-23 12:16:37.610 16910-16910/ph.coredev.johnjessbayutas.mobileportal D/AndroidRuntime: Shutting down VM
05-23 12:16:37.610 16910-16910/ph.coredev.johnjessbayutas.mobileportal W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40acb300)
05-23 12:16:37.620 16910-16910/ph.coredev.johnjessbayutas.mobileportal E/AndroidRuntime: FATAL EXCEPTION: main
 java.lang.NullPointerException
     at ph.coredev.johnjessbayutas.mobileportal.Activity.LoginActivity.checkLogin(LoginActivity.java:187)
     at ph.coredev.johnjessbayutas.mobileportal.Activity.LoginActivity.access$200(LoginActivity.java:35)
     at ph.coredev.johnjessbayutas.mobileportal.Activity.LoginActivity$1.onClick(LoginActivity.java:83)
     at android.view.View.performClick(View.java:4084)
     at android.view.View$PerformClick.run(View.java:16966)
     at android.os.Handler.handleCallback(Handler.java:615)
     at android.os.Handler.dispatchMessage(Handler.java:92)
     at android.os.Looper.loop(Looper.java:137)
     at android.app.ActivityThread.main(ActivityThread.java:4746)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:511)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
     at dalvik.system.NativeStart.main(Native Method)
05-23 12:21:37.720 16910-16910/ph.coredev.johnjessbayutas.mobileportal I/Process: Sending signal. PID: 16910 SIG: 9

我很好奇这个错误第二行:

05-23 12:16:28.930 16910-16910/ph.coredev.johnjessbayutas.mobileportal E/Trace: error opening trace file: No such file or directory (2)

0 个答案:

没有答案