2

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

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 将要执行时,程序将关闭并提示错误。

4

4 回答 4

2

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

  MainActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(MainActivity.this,"call your toast from the main ui thread",300).show();
                                    }
                                });
于 2013-02-09T16:32:57.607 回答
1

doInBackground是一个后台非 UI 线程,您不能从那里进行 UI 活动

另一方面,UI 线程是 onPreExecute、onProgressUpdate 和 onPostExecute。您的 UI 活动应该只发生在这三个功能之一中。

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

于 2013-02-09T14:46:15.347 回答
0

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

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

于 2013-02-09T14:42:40.980 回答
0

您不能从异步任务的 onBackground 方法所在的后台线程 Toast。http://developer.android.com/reference/android/os/AsyncTask.html#doInBackground(Params...)

例如,您可以覆盖

protected void onPostExecute (Result result)

或者

protected void onProgressUpdate (Progress... values)

和 Toast,因为它们在 UI 线程上运行。

于 2013-02-09T14:44:01.747 回答