dongye4192
dongye4192
2014-09-04 19:14

NullPointerException NetAsync错误:在联系服务器屏幕上应用程序始终崩溃[关闭]

  • java
  • android
  • php
  • registration
  • nullpointerexception

I'm working on connecting mobile app and server Apache. I tried debug and solved all the misspelling, I think. This is my code, please help:

    @Override
     protected void onPostExecute(JSONObject json) {
        // Checks for success message
        try {
            Log.e(KEY_ERROR, "Comes to Here");
            if (json.getString(KEY_SUCCESS) != null) {
                registerErrorMsg.setText("");
                String res = json.getString(KEY_SUCCESS);
                String red = json.getString(KEY_ERROR);
                if (Integer.parseInt(res) == 1) {
                    pDialog.setTitle("Getting Data");
                    pDialog.setMessage("Loading Info");
                    registerErrorMsg.setText("Successfully Registerd");
                    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                    JSONObject json_user = json.getJSONObject("user");

                    // Removes all the previous data in the SQlite database
                    UserFunctions logout = new UserFunctions();
                    logout.logoutUser(getApplicationContext());
                    db.addUser(json_user.getString(KEY_FIRSTNAME), json_user.getString(KEY_LASTNAME),   json_user.getString(KEY_EMAIL), json_user.getString(KEY_USERNAME), json_user.getString(KEY_UID),  json_user.getString(KEY_CREATED_AT));

                    // Stores registered data in SQLite Database
                    //Launch Registered screen
                    Intent registered = new Intent(getApplicationContext(), Registered.class);

                    // Close all views before launching Registered Screen
                    registered.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    pDialog.dismiss();
                    startActivity(registered);
                    finish();
                } else if (Integer.parseInt(red) == 2) {
                    pDialog.dismiss();
                    registerErrorMsg.setText("User already exists");
                } else if (Integer.parseInt(red) == 3) {
                    pDialog.dismiss();
                    registerErrorMsg.setText("Invalid Email id");
                } else {
                    pDialog.dismiss();
                    registerErrorMsg.setText("Error occurred in registration");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

public void NetAsync(View view){
    new NetCheck().execute();
}

logcat:

09-04 15:07:53.245    1995-1995/com.knedl.testloginapp E/error﹕ Comes to Here
09-04 15:07:53.245    1995-1995/com.knedl.testloginapp D/AndroidRuntime﹕ Shutting down VM
09-04 15:07:53.245    1995-1995/com.knedl.testloginapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2cc1b20)
09-04 15:07:53.245    1995-1995/com.knedl.testloginapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.knedl.testloginapp, PID: 1995
java.lang.NullPointerException
        at com.knedl.testloginapp.Register$ProcessRegister.onPostExecute(Register.java:181)
        at com.knedl.testloginapp.Register$ProcessRegister.onPostExecute(Register.java:149)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        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:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

Sorry i'm new here. I get the json JSONObject from here:

  private class ProcessRegister extends AsyncTask<String, String, JSONObject> {
    // Defining Process dialog
    private ProgressDialog pDialog;
    String email, password, fname, lname, uname;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        fname = inputFirstName.getText().toString();
        lname = inputLastName.getText().toString();
        email = inputEmail.getText().toString();
        uname = inputUsername.getText().toString();
        password = inputPassword.getText().toString();
        pDialog = new ProgressDialog(Register.this);
        pDialog.setTitle("Contacting Servers");
        pDialog.setMessage("Registering ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args) {
        UserFunctions userFunctions = new UserFunctions();
        JSONObject json = userFunctions.registerUser(fname, lname, email, uname, password);
        return json;
    }

And this is method registerUser from userFunctions library. I get empty json when I go through debug:

 public JSONObject registerUser(String fname, String lname, String email, String uname, String password){
    // Building Parameters
    List params = new ArrayList();
    params.add(new BasicNameValuePair("tag", register_tag));
    params.add(new BasicNameValuePair("fname", fname));
    params.add(new BasicNameValuePair("lname", lname));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("uname", uname));
    params.add(new BasicNameValuePair("password", password));
    JSONObject json = jsonParser.getJSONFromUrl(registerURL,params);
    return json;
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐