-2

我是 android 新手,我已经分配了一个项目。我在查看错误日志时遇到了问题。这是我的错误日志,请告诉我这个问题是什么以及如何解决

03-13 12:40:06.470: E/WindowManager(923): android.view.WindowLeaked: Activity  om.example.mytest.ReadContactsActivity has leaked window   com.android.internal.policy.impl.PhoneWindow$DecorView{40d0e768 V.E..... R.....ID 0,0-97,92} that was originally added here
03-13 12:40:06.470: E/WindowManager(923):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
03-13 12:40:06.470: E/WindowManager(923):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
03-13 12:40:06.470: E/WindowManager(923):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
03-13 12:40:06.470: E/WindowManager(923):   at android.app.Dialog.show(Dialog.java:281)
03-13 12:40:06.470: E/WindowManager(923):   at com.example.mytest.ReadContactsActivity$CountDownTask.onPreExecute(ReadContactsActivity.java:50)
03-13 12:40:06.470: E/WindowManager(923):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
03-13 12:40:06.470: E/WindowManager(923):   at android.os.AsyncTask.execute(AsyncTask.java:534)
03-13 12:40:06.470: E/WindowManager(923):   at com.example.mytest.ReadContactsActivity.onCreate(ReadContactsActivity.java:34)
03-13 12:40:06.470: E/WindowManager(923):   at android.app.Activity.performCreate(Activity.java:5104)
03-13 12:40:06.470: E/WindowManager(923):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-13 12:40:06.470: E/WindowManager(923):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-13 12:40:06.470: E/WindowManager(923):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-13 12:40:06.470: E/WindowManager(923):   at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-13 12:40:06.470: E/WindowManager(923):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-13 12:40:06.470: E/WindowManager(923):   at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 12:40:06.470: E/WindowManager(923):   at android.os.Looper.loop(Looper.java:137)
03-13 12:40:06.470: E/WindowManager(923):   at android.app.ActivityThread.main(ActivityThread.java:5039)
03-13 12:40:06.470: E/WindowManager(923):   at java.lang.reflect.Method.invokeNative(Native Method)
03-13 12:40:06.470: E/WindowManager(923):   at java.lang.reflect.Method.invoke(Method.java:511)
03-13 12:40:06.470: E/WindowManager(923):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-13 12:40:06.470: E/WindowManager(923):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-13 12:40:06.470: E/WindowManager(923):   at dalvik.system.NativeStart.main(Native Method)

这是我为实现而编写的代码

 private class CountDownTask extends AsyncTask<Void, String, Void>{


    // A callback method executed on UI thread on starting the task
    @Override
    protected void onPreExecute() {
        // Getting reference to the TextView tv_counter of the layout activity_main
        dialog = new Dialog(ReadContactsActivity.this);
        dialog.setCancelable(false);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.progressdialog);

        progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar1);
        dialog.show();


    }

    // A callback method executed on non UI thread, invoked after 
    // onPreExecute method if exists

    protected Void doInBackground(Void... params) {
         ContentResolver cr = ReadContactsActivity.this.getContentResolver();
         Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);

         if (cur.getCount() > 0) {
             while (cur.moveToNext()) {
                 String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                 String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                 if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                     Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                            new String[]{id}, null);
                     while (pCur.moveToNext()) {
                           String phone = pCur.getString(
                                  pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                           objItem = new listBean();
                        objItem.setName(name);
                        objItem.setNumber(phone);
                        listArray.add(objItem);
                     }
                     pCur.close();
                     }
                 }
             }
        return null;
    }


    // A callback method executed on UI thread, invoked by the publishProgress() 
    // from doInBackground() method
    @Override
    protected void onProgressUpdate(String... values) {

    }


    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
        objAdapter = new listAdapter(ReadContactsActivity.this, items);
        listView.setAdapter(objAdapter);

        }   
4

2 回答 2

1

不要在主线程上编写关闭对话框的代码。将其保留在 runOnUIThread 上,如下所示:

 @Override
   protected void onPostExecute(Void result) {
      runOnUiThread(new Runnable() {
                @Override
               public void run() {
                      dialog.dismiss();
            }
     objAdapter = new listAdapter(ReadContactsActivity.this, items);
     listView.setAdapter(objAdapter);
    }
于 2013-03-13T13:00:56.043 回答
1

如何知道android编码的问题在哪里?

如果您的活动代码中有错误,您总是可以在 logcat 中找到带有行号的活动名称。

于 2013-03-13T12:49:58.797 回答