异步任务中的错误

发布于 2024-12-04 17:12:42 字数 8204 浏览 0 评论 0原文

在 logcat 中出现此错误

09-15 14:38:09.944: ERROR/AndroidRuntime(1065): FATAL EXCEPTION: AsyncTask #1
09-15 14:38:09.944: ERROR/AndroidRuntime(1065): java.lang.RuntimeException: An error occured while executing doInBackground()
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.lang.Thread.run(Thread.java:1096)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.os.Handler.<init>(Handler.java:121)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.app.Dialog.<init>(Dialog.java:101)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.app.AlertDialog.<init>(AlertDialog.java:63)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.app.AlertDialog.<init>(AlertDialog.java:59)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.app.AlertDialog$Builder.create(AlertDialog.java:786)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at com.UserLogin.UserLogin$CheckUser.doInBackground(UserLogin.java:198)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at com.UserLogin.UserLogin$CheckUser.doInBackground(UserLogin.java:1)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     ... 4 more

AsyncTask代码

private class CheckUser extends AsyncTask<String, Void, Void> {
        private AlertDialog AD1;

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            HttpClient httpclient = new DefaultHttpClient();
            // Dh.getWritableDatabase();

            HttpPost httppost = new HttpPost(url);
            List<NameValuePair> params1 = new ArrayList<NameValuePair>(2);
            params1.add(new BasicNameValuePair("username", Username));

            try {
                httppost.setEntity(new UrlEncodedFormEntity(params1));
                Log.i("Params",EntityUtils.toString(new UrlEncodedFormEntity(params1)));
                HttpResponse responseGet = httpclient.execute(httppost);
                HttpEntity entity = responseGet.getEntity();
                if (entity != null) {

                    InputStream inputStreamResponse = entity.getContent();
                    result = convertStreamToString(inputStreamResponse);
                    System.out.println(result);
                    db = InsertData.getWritableDatabase();
                    ContentValues values = new ContentValues();

                    MyUserJsonObject = new JSONObject(result);

                    JSONObject menuObject = MyUserJsonObject.getJSONObject("UserDetails");
                    attributeId = menuObject.getString("login");

                    System.out.println(attributeId);
                    if (attributeId.equals("success")) {
                        JSONObject FriendObject = menuObject.getJSONObject("friendslist");
                        JSONArray FriendArray = FriendObject.getJSONArray("friends");

                        for (int i = 0; i < FriendArray.length(); i++) {

                            values.put(DatabaseHelper.FriendName, FriendArray.getJSONObject(i).getString("FriendName").toString());
                            values.put(DatabaseHelper.FriendMobile, FriendArray.getJSONObject(i).getString("Mobile").toString());
                            db.insert(DatabaseHelper.FRIENDTABLE, null, values);

                        }
                        values.clear();

                        JSONObject PlaceObjet = menuObject.getJSONObject("findplaces");
                        JSONArray PlacesArray = PlaceObjet.getJSONArray("places");
                        for (int i = 0; i < PlacesArray.length(); i++) {
                            values.put(DatabaseHelper.Name, PlacesArray.getJSONObject(i).getString("Name").toString());
                            values.put(DatabaseHelper.Image,PlacesArray.getJSONObject(i).getString("Image").toString());
                            db.insert(DatabaseHelper.LOCATIONTABLE, null,values);
                        }
                        values.clear();
                        db.close();
                        // System.out.println(friendslist);

                    } else {
                        dialog1.dismiss();

                    }
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            catch (IOException e) {
                // TODO Auto-generated catch block
                // e.printStackTrace();
                AlertDialog ADl = new AlertDialog.Builder(
                        getApplicationContext()).create();
                ADl.setTitle("Login Failed..");
                ADl.setMessage("You have Entered Wrong Username/Password Please try again");
                ADl.setIcon(R.drawable.oops);
                ADl.setButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.cancel();
                    }
                });
                ADl.show();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            dialog1.dismiss();
            if(attributeId.equals("success")){
            Intent innerintent = new Intent(getApplicationContext(),Innerpage.class);
            startActivity(innerintent);
            } else {
                 dialog1.dismiss();
                TextView Tv=(TextView)findViewById(R.id.error);
                 Tv.setText("Please Enter Valid Username/password");
             }
            if(attributeId.equals("success")){ 
                    Intent innerintent = new Intent(getApplicationContext(),Innerpage.class);
                    startActivity(innerintent);
                } else {
                    AD1= new AlertDialog.Builder(getApplicationContext()).create();
                    ADl.setTitle("Login Failed.."); ADl.setMessage("You have Entered Wrong Username/Password Please try again");
                    ADl.setIcon(R.drawable.oops); ADl.setButton("Ok", new
                            DialogInterface.OnClickListener() {

                        @Override public void onClick(DialogInterface dialog, int which)
                        { //TODO Auto-generated method stub 
                            dialog.cancel(); 
                        } 
                    });
                }
                ADl.show(); 
            }       
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            dialog1 = ProgressDialog.show(UserLogin.this, "","Please wait for few seconds...", true);
            dialog1.show();
        }
    }

getting this error in logcat

09-15 14:38:09.944: ERROR/AndroidRuntime(1065): FATAL EXCEPTION: AsyncTask #1
09-15 14:38:09.944: ERROR/AndroidRuntime(1065): java.lang.RuntimeException: An error occured while executing doInBackground()
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.lang.Thread.run(Thread.java:1096)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.os.Handler.<init>(Handler.java:121)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.app.Dialog.<init>(Dialog.java:101)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.app.AlertDialog.<init>(AlertDialog.java:63)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.app.AlertDialog.<init>(AlertDialog.java:59)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.app.AlertDialog$Builder.create(AlertDialog.java:786)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at com.UserLogin.UserLogin$CheckUser.doInBackground(UserLogin.java:198)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at com.UserLogin.UserLogin$CheckUser.doInBackground(UserLogin.java:1)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-15 14:38:09.944: ERROR/AndroidRuntime(1065):     ... 4 more

AsyncTask Code

private class CheckUser extends AsyncTask<String, Void, Void> {
        private AlertDialog AD1;

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            HttpClient httpclient = new DefaultHttpClient();
            // Dh.getWritableDatabase();

            HttpPost httppost = new HttpPost(url);
            List<NameValuePair> params1 = new ArrayList<NameValuePair>(2);
            params1.add(new BasicNameValuePair("username", Username));

            try {
                httppost.setEntity(new UrlEncodedFormEntity(params1));
                Log.i("Params",EntityUtils.toString(new UrlEncodedFormEntity(params1)));
                HttpResponse responseGet = httpclient.execute(httppost);
                HttpEntity entity = responseGet.getEntity();
                if (entity != null) {

                    InputStream inputStreamResponse = entity.getContent();
                    result = convertStreamToString(inputStreamResponse);
                    System.out.println(result);
                    db = InsertData.getWritableDatabase();
                    ContentValues values = new ContentValues();

                    MyUserJsonObject = new JSONObject(result);

                    JSONObject menuObject = MyUserJsonObject.getJSONObject("UserDetails");
                    attributeId = menuObject.getString("login");

                    System.out.println(attributeId);
                    if (attributeId.equals("success")) {
                        JSONObject FriendObject = menuObject.getJSONObject("friendslist");
                        JSONArray FriendArray = FriendObject.getJSONArray("friends");

                        for (int i = 0; i < FriendArray.length(); i++) {

                            values.put(DatabaseHelper.FriendName, FriendArray.getJSONObject(i).getString("FriendName").toString());
                            values.put(DatabaseHelper.FriendMobile, FriendArray.getJSONObject(i).getString("Mobile").toString());
                            db.insert(DatabaseHelper.FRIENDTABLE, null, values);

                        }
                        values.clear();

                        JSONObject PlaceObjet = menuObject.getJSONObject("findplaces");
                        JSONArray PlacesArray = PlaceObjet.getJSONArray("places");
                        for (int i = 0; i < PlacesArray.length(); i++) {
                            values.put(DatabaseHelper.Name, PlacesArray.getJSONObject(i).getString("Name").toString());
                            values.put(DatabaseHelper.Image,PlacesArray.getJSONObject(i).getString("Image").toString());
                            db.insert(DatabaseHelper.LOCATIONTABLE, null,values);
                        }
                        values.clear();
                        db.close();
                        // System.out.println(friendslist);

                    } else {
                        dialog1.dismiss();

                    }
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            catch (IOException e) {
                // TODO Auto-generated catch block
                // e.printStackTrace();
                AlertDialog ADl = new AlertDialog.Builder(
                        getApplicationContext()).create();
                ADl.setTitle("Login Failed..");
                ADl.setMessage("You have Entered Wrong Username/Password Please try again");
                ADl.setIcon(R.drawable.oops);
                ADl.setButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.cancel();
                    }
                });
                ADl.show();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            dialog1.dismiss();
            if(attributeId.equals("success")){
            Intent innerintent = new Intent(getApplicationContext(),Innerpage.class);
            startActivity(innerintent);
            } else {
                 dialog1.dismiss();
                TextView Tv=(TextView)findViewById(R.id.error);
                 Tv.setText("Please Enter Valid Username/password");
             }
            if(attributeId.equals("success")){ 
                    Intent innerintent = new Intent(getApplicationContext(),Innerpage.class);
                    startActivity(innerintent);
                } else {
                    AD1= new AlertDialog.Builder(getApplicationContext()).create();
                    ADl.setTitle("Login Failed.."); ADl.setMessage("You have Entered Wrong Username/Password Please try again");
                    ADl.setIcon(R.drawable.oops); ADl.setButton("Ok", new
                            DialogInterface.OnClickListener() {

                        @Override public void onClick(DialogInterface dialog, int which)
                        { //TODO Auto-generated method stub 
                            dialog.cancel(); 
                        } 
                    });
                }
                ADl.show(); 
            }       
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            dialog1 = ProgressDialog.show(UserLogin.this, "","Please wait for few seconds...", true);
            dialog1.show();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2024-12-11 17:12:42

您正在尝试在 asynctask 的 doInBackground 方法内构建 AlertDialog。

除非你先准备一个循环器或其他东西(这很复杂),否则你不能这样做。

相反,可以在 onProgressUpdate 或 onPostExecute 方法中创建 AlertDialog,或者在 UI 线程上运行的 Runnable 中创建(例如,通过 runOnUIThread)

You are trying to build an AlertDialog inside the doInBackground method of your asynctask.

You can't do that unless you first prepare a looper or something (which is complex).

Instead, create your AlertDialog either in the onProgressUpdate or onPostExecute method, or in a Runnable that runs on the UI thread (via runOnUIThread, for example)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文