Android AsyncTask onPostExecute启动新活动崩溃app

时间:2017-03-09 19:07:17

标签: android android-intent android-asynctask

我尝试了很多东西让它发挥作用,但它并没有帮助。我有一个用户注册活动是先启动。用户注册运行良好但在onPostExecute上我无法开始新的活动。

我尝试了这些代码。

Intent intent = new Intent(getApplicationContext(), starting.class);
startActivity(intent);
//OR    
Intent intent = new Intent(membership.this, starting.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);

我的代码在底部。

        class RegisterUser extends AsyncTask<String, Void, String> {
        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(membership.this, "Lütfen Bekleyiniz!",null, true, true);
        }
        @Override
        protected void onPostExecute(String isDone) {
            super.onPostExecute(isDone);
            loading.dismiss();
            Toast.makeText(getApplicationContext(),isDone, Toast.LENGTH_LONG).show();
            //
            //CONTROL DATA
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("USER_CONTROL", true);
            editor.commit();
            //
            //I start activiy in here.
        }
        @Override
        protected String doInBackground(String... params) {
            String s = params[0];
            BufferedReader bufferedReader = null;
            try {
                URL url = new URL(REGISTER_URL+s);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String result;
                result = bufferedReader.readLine();
                return result;


            }catch(Exception e){
                return null;
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您需要在sharedPreferences

中初始化onCreate()
 public static final String MyPREFERENCES = "MyPrefs";

 prefs = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

问题是您的prefs为空

答案 1 :(得分:0)

试试这个。由于共享偏好而您不是因为意图而面临崩溃。

 SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(membership.this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("USER_CONTROL", true);
    editor.apply();