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

时间:2013-03-13 12:46:57

标签: android

我是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);

        }   

2 个答案:

答案 0 :(得分:1)

  

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

如果您的活动代码中存在错误,您始终会在logcat中找到活动名称,行号

答案 1 :(得分: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);
    }