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

时间:2013-10-09 10:41:47

标签: java android eclipse

这是我正在处理的代码。当我使用此代码运行我的应用程序时,它会停止,没有错误。

我已经更改了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();
                }
            }

} 

5 个答案:

答案 0 :(得分:1)

您必须在UI Thread上进行查看操作。背景线程上的异步doInBackground方法。你没有在后台线程中获得editText的文本。

答案 1 :(得分:0)

试试这个..删除finish();

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

答案 2 :(得分:0)

你在doinbackground()方法中返回null而不是返回有效用户并在onPostExecute中

protected void onPostExecute(String validUser) {

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


         params.clear();
            }
        }

答案 3 :(得分:0)

这样做

 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();
                }
            }

}***

答案 4 :(得分:0)

我做了一些改变和建议。请参阅以下代码。

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();
                }
            }

}