启动新活动会导致应用崩溃

时间:2015-12-31 23:38:23

标签: java android-studio

我试图在AsyncTask之后在onPostExecute()方法内启动一个新的Intent。该应用程序总是崩溃,但没有错误日志。 System.out.println()次调用会返回预期的输出。

 @Override
        protected JSONObject doInBackground(Void... params) {
            // TODO: attempt authentication against a network service.


            try {
                // Use the email address minus the '@' prefix as temp username.
                String[] uList = (mEmail.split("@"));
                String mUsername = uList[0];

                Serverrequest request = new Serverrequest();
                JSONObject userObj = request.getJSON(
                        "http://10.0.2.2:3700/api/login", mUsername, mPassword);
                if (userObj != null) {
                    // Account exists, return user
                    return userObj;

                } else {
                    System.out.println("Null response");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            // TODO: register the new account here.
            JSONObject newUser = new JSONObject();
            try {
                newUser.put("username", "Charlie");

            } catch (JSONException e) {

            }
            return newUser;
        }



@Override
        protected void onPostExecute(final JSONObject user) {
            try {
                mAuthTask = null;
                showProgress(false);
                System.out.println(user);
                String username = user.getString("username");
                System.out.println(username);

            SharedPreferences prefs = getSharedPreferences("wordsmith", MODE_PRIVATE);
            prefs.edit().putString("username", username).apply();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            Intent intent = new Intent(LoginActivity.this, StartScreenActivity.class);
            intent.putExtra("user", user.toString());
            LoginActivity.this.finish();
            startActivity(intent);

0 个答案:

没有答案