-1

再说一次,在我之前的帖子中,有人告诉我在代码中使用 AsyncTask,以避免 mainexceptionthread,现在我遇到了这个错误:

**

01-26 09:39:06.220: E/AndroidRuntime(17218): FATAL EXCEPTION: AsyncTask #1
01-26 09:39:06.220: E/AndroidRuntime(17218): java.lang.RuntimeException: An error occured while executing doInBackground()
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at java.lang.Thread.run(Thread.java:856)
01-26 09:39:06.220: E/AndroidRuntime(17218): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.view.View.invalidate(View.java:10193)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.widget.TextView.invalidateRegion(TextView.java:4375)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.widget.TextView.invalidateCursor(TextView.java:4318)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.widget.TextView.spanChange(TextView.java:7172)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:8759)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:979)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:688)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.text.Selection.setSelection(Selection.java:76)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.text.Selection.setSelection(Selection.java:87)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.widget.TextView.setText(TextView.java:3535)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.widget.TextView.setText(TextView.java:3405)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.widget.EditText.setText(EditText.java:80)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.widget.TextView.setText(TextView.java:3380)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at com.example.projectthesis.Main$phpconnect.doInBackground(Main.java:98)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at com.example.projectthesis.Main$phpconnect.doInBackground(Main.java:1)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-26 09:39:06.220: E/AndroidRuntime(17218):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-26 09:39:06.220: E/AndroidRuntime(17218):    ... 5 more

**

我认为这是因为 Exception e 部分,它有 inputEmail.setText(e.toString()); 但是当我将它更改为仅 e.printstacktrace 时,它​​没有任何结果。你能帮我看看这些是我的代码:

安卓:

**

public class Main extends Activity {
    EditText inputEmail;
    EditText inputPassword;
    Button btnLogin;
    private ProgressDialog pDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        inputEmail = (EditText) findViewById(R.id.inputEmail);
        inputPassword = (EditText) findViewById(R.id.inputPassword);
        Button btnLogin = (Button) findViewById(R.id.btnLogin);

        // button click event
        btnLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating new product in background thread
                validation();
            }
        }); 
    }

    public void validation()
    {
        if(inputEmail.getText().toString().equals("") || inputPassword.getText().toString().equals(""))
                {
                Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show();
                }
        else
        {
             new phpconnect().execute();
        }
        }


    class phpconnect extends AsyncTask<String, String, String>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Main.this);
            pDialog.setMessage("Logging in..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
             ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("eadd", inputEmail.getText().toString()));
                postParameters.add(new BasicNameValuePair("password", inputPassword.getText().toString()));
                //Passing Parameter to the php web service for authentication
                //String valid = "1";
                String response = null;
                try {
                response = CustomHttpClient.executeHttpPost("http://10.0.2.2/TheCalling/log_in.php", postParameters);  //Enter Your remote PHP,ASP, Servlet file link
                String res=response.toString();
                //res = res.trim();
                res= res.replaceAll("\\s+","");
                //error.setText(res);
                if(res.equals("1"))
                {
                    Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show();
                    Intent i = new Intent(Main.this,MainMenu.class);
                    startActivity(i);
                }
                    else
                        if(res.equals("0"))
                    {
                    Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show();
                    }
                } catch (Exception e) {
                    inputEmail.setText(e.toString());
                }
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        } 

    }


}

**

这又是我的 PHP 代码:

**

<?php
include("db_config.php");
$eadd=addslashes($_POST['eadd']);
$password=addslashes($_POST['password']);
$sql="SELECT * FROM users WHERE eadd='$eadd' and password='$password'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$count=mysql_num_rows($result);
if($count==1)
    {
    echo "1";
    //(If result found send 1 to android)
    }
else
    {
    echo "0";
    //(If result not found send o to android)
    }
?>

**

4

2 回答 2

0

由于以下原因,您会收到错误:

catch (Exception e) {
    inputEmail.setText(e.toString());
}

在这里,您尝试通过从 AsyncTask 的后台线程更改 EditText 的内容来更改应用程序的 UI。由于我怀疑您是否真的想在电子邮件输入字段中向用户显示错误并且这样做只是为了调试目的,请尝试使用e.printStackTrace()而不是inputEmail.setText(e.toString());

此外,您需要将Toast.show()调用包装在runOnUiThread()可运行文件中。

于 2013-01-28T04:39:59.670 回答
0

您不能将 toast 消息doInBackground移至onPostExecute. 是的,这也是

catch (Exception e) {
                    inputEmail.setText(e.toString());
                }
于 2013-01-28T04:40:42.820 回答