在asynctask内吐司

时间:2013-02-09 14:35:22

标签: android android-asynctask toast

请帮助如何使用这个。我正在登录并验证所有错误和可能的异常。这是我的代码`

EditText un, pw;
ImageButton login;
TextView error;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    un=(EditText)findViewById(R.id.txtUsername);
    pw=(EditText)findViewById(R.id.txtPassword);
    login=(ImageButton)findViewById(R.id.btnLogin);
    error=(TextView)findViewById(R.id.errTxt);


    login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub


            new login().execute();


        }



    });


}
public class login extends AsyncTask{

    @Override
    protected Object doInBackground(Object... params) {
        // TODO Auto-generated method stub

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
        postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
        //String valid = "1";
        String response = null;


        try {

            response = CustomHttpClient.executeHttpPost("http://www.cjcld.org/dvts1/mobile/login.php", postParameters);
            String res=response.toString();
           // res = res.trim();
            res= res.replaceAll("\\s+","");                              
            //error.setText(res);

           if(Integer.parseInt(res)>0){

            postParameters.add(new BasicNameValuePair("id", res));
             String result = null;
           try
           {
              result = CustomHttpClient.executeHttpPost("http://www.cjcld.org/dvts1/mobile/status.php", postParameters);
              String res2=result.toString();
              res2= res2.replaceAll("\\s+","");    

              if(Integer.parseInt(res2)>0){

               Intent myIntent = new Intent(getApplicationContext(), dashboard.class); 
               Bundle logval = new Bundle();

              logval.putString("uid", res);
              logval.putString("did", res2);
               myIntent.putExtras(logval);
               startActivity(myIntent);
              }else{

                  Toast.makeText(getApplicationContext(),"No Delivery", Toast.LENGTH_LONG).show();
              }
           }
           catch(Exception e)           
           {

               Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
           }
           }
            else
            {

                Toast.makeText(getApplicationContext(),"Sorry!! Incorrect Username or Password", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {

                Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
        }
        return null;
    }



}

当吐司要执行时,程序将关闭并提示错误。

4 个答案:

答案 0 :(得分:2)

您需要在主Userfaceface线程中执行此操作。像这样

  MainActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(MainActivity.this,"call your toast from the main ui thread",300).show();
                                    }
                                });

答案 1 :(得分:1)

doInBackground是一个后台非UI线程,您无法从那里执行UI活动

另一方面,UI线程是onPreExecute,onProgressUpdate和onPostExecute。您的UI活动应仅在以下三个函数之一中发生。

您应该尝试在doInBackground()中设置一个标志,然后检查onPostExecute中的标志值并相应地显示Toast消息。

答案 2 :(得分:0)

由于您没有发布错误,因此无法确定,但根据经验,您不应该从后台线程修改UI 。我猜一个吐司有资格作为UI修改吗?

尝试从onPostExecuteonProgressUpdate方法启动Toast(以及仪表板活动的意图)

答案 3 :(得分:0)

你不能从后台线程Toast,这是Async Task的onBackground方法。 http://developer.android.com/reference/android/os/AsyncTask.html#doInBackground(Params...)

您可以覆盖

protected void onPostExecute (Result result)

protected void onProgressUpdate (Progress... values)

和来自那里的Toast,因为它们在UI线程上运行。

相关问题