简体   繁体   English

如何从Android中的PostExecute启动新意图?

[英]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. 我更改了manifest.xml,但无法正常工作。

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. 您必须在UIThread上执行视图操作。 Async doInBackground method on background thread. 后台线程上的async doInBackground方法。 You don't get editText' s text in background thread. 您不会在后台线程中获得editText的文本。

Try this.. Remove the finish(); 试试这个..删除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 您在doinbackground()方法中返回null,而是在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();
                }
            }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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