开发者

error in asynchtask

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();
        }
    }


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜