简体   繁体   中英

How to start new intent from PostExecute in Android?

Here is the code I'm working on. When I'm running my app with this code, it's getting stopped, with no errors.

I have changed manifest.xml, but it's not working.

public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ok=(Button)findViewById(R.id.Button01);  
        ok.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View arg0) { 
                new jsdetails().execute();


                }

        });
    }
    public class jsdetails extends AsyncTask<String,String,String> {

        Boolean validUser = false;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Verifying.. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected String doInBackground(String... args) {

            //int valoreOnPostExecute = 0;
            error=(TextView)findViewById(R.id.TextView01);
            un=(EditText)findViewById(R.id.EditText01);  
            pw=(EditText)findViewById(R.id.EditText02); 

            params.add(new BasicNameValuePair("JS_Email",un.getText().toString()));  
            params.add(new BasicNameValuePair("JS_Password",pw.getText().toString()));

            JSONObject json = jParser.makeHttpRequest(url,"POST",params); 
            Log.d("All Products: ", json.toString());
                try {  
             int success = json.getInt(TAG_SUCCESS);
                                 if (success == 1)
                 {
                     validUser = true;

                 }
                                } catch (JSONException e) {
                e.printStackTrace();
            }
                 return null;
        }
        protected void onPostExecute(String file_url) {

            pDialog.dismiss();
            if(validUser)
            {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
                finish();
            }


             params.clear();
                }
            }

} 

You must make view action on UIThread. Async doInBackground method on background thread. You don't get editText' s text in background thread.

Try this.. Remove the finish();

if(validUser)
{
      Intent i = new Intent(MainActivity.this, UserManage.class);
      startActivity(i);
}

You are returning null in doinbackground() method instead return valid user and in onPostExecute

protected void onPostExecute(String validUser) {

        pDialog.dismiss();
        if(validUser)
        {
            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(i);
            finish();
        }


         params.clear();
            }
        }

Do it like this

 public class jsdetails extends AsyncTask<String,String,Boolean> {

        Boolean validUser = false;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Verifying.. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected Boolean doInBackground(String... args) {

            //int valoreOnPostExecute = 0;
            error=(TextView)findViewById(R.id.TextView01);
            un=(EditText)findViewById(R.id.EditText01);  
            pw=(EditText)findViewById(R.id.EditText02); 

            params.add(new BasicNameValuePair("JS_Email",un.getText().toString()));  
            params.add(new BasicNameValuePair("JS_Password",pw.getText().toString()));

            JSONObject json = jParser.makeHttpRequest(url,"POST",params); 
            Log.d("All Products: ", json.toString());
                try {  
             int success = json.getInt(TAG_SUCCESS);
                                 if (success == 1)
                 {
                     validUser = true;

                 }
                                } catch (JSONException e) {
                e.printStackTrace();
            }
                 return validUser;
        }
        protected void onPostExecute(Boolean validUser) {

            pDialog.dismiss();
            if(validUser)
            {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
                finish();
            }


             params.clear();
                }
            }

}***

I made a few changes and suggestions. Refer following code.

public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            error=(TextView)findViewById(R.id.TextView01);
            un=(EditText)findViewById(R.id.EditText01);      // Note
            pw=(EditText)findViewById(R.id.EditText02);
        ok=(Button)findViewById(R.id.Button01);  
        ok.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View arg0) { 
                new jsdetails(MainActivity.this).execute(un.getText().toString(),pw.getText().toString());
// Note

                }

        });
    }
    public class jsdetails extends AsyncTask<String,Integer,Boolean > {    // Note
        Context context;    // Note
        Boolean validUser = false;
        jsdetails(Context context){    // Note
           this.context=context;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Verifying.. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected Boolean doInBackground(String... args) {    // Note

            //int valoreOnPostExecute = 0;


            params.add(new BasicNameValuePair("JS_Email",args[0]));      // Note
            params.add(new BasicNameValuePair("JS_Password",args[1]));

            JSONObject json = jParser.makeHttpRequest(url,"POST",params); 
            Log.d("All Products: ", json.toString());
                try {  
             int success = json.getInt(TAG_SUCCESS);
                                 if (success == 1)
                 {
                     validUser = true;

                 }
                                } catch (JSONException e) {
                e.printStackTrace();
            }
                 return validUser ;
        }
        protected void onPostExecute(String result) {

            pDialog.dismiss();
            if(result)    // Note
            {
                Intent i = new Intent(context, UserManage.class);    // Note
                startActivity(i);
                finish();
            }


             params.clear();
                }
            }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM