android - 异步任务中的 toast

标签 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;
    }



}

当toast要执行时,程序会关闭并提示错误。

最佳答案

您需要在主用户界面线程中执行此操作。像这样

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

关于android - 异步任务中的 toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14789169/

相关文章:

Android为Toast添加自定义动画

java - 安卓/Java : how to set background color of MaterialShapeDrawable with int Color?

java - 查找特定日期某个时间的天数、小时数和分钟数

android - 设置 DialogPreference 摘要 OnPreferenceChange

用于将 GPS 坐标发送到服务器的 Android 应用程序崩溃

java - 在 fragment 开始之前显示 toast

android - 将整个手机的联系人列表添加到应用程序数据库中

android - 无法通过 `cls.newInstance()` 创建异步任务的实例?

java - 无法在 doInbackground() 方法中执行 AlertDialog

android - 如何在 Android 中设置 toast 的宽度?