มุมมองไม่แนบกับตัวจัดการหน้าต่างที่ขัดข้อง


189

ฉันใช้ ACRA เพื่อรายงานแอปขัดข้อง ฉันได้รับView not attached to window managerข้อความแสดงข้อผิดพลาดและคิดว่าได้แก้ไขโดยpDialog.dismiss();ใส่คำสั่ง if ใน:

if (pDialog!=null) 
{
    if (pDialog.isShowing()) 
    {
        pDialog.dismiss();   
    }
}

มันลดปริมาณการView not attached to window managerล่มที่ฉันได้รับ แต่ฉันยังคงได้รับอยู่บ้างและฉันไม่แน่ใจว่าจะแก้ไขได้อย่างไร

ข้อความผิดพลาด:

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:425)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:327)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:83)
at android.app.Dialog.dismissDialog(Dialog.java:330)
at android.app.Dialog.dismiss(Dialog.java:312)
at com.package.class$LoadAllProducts.onPostExecute(class.java:624)
at com.package.class$LoadAllProducts.onPostExecute(class.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)

ข้อมูลโค้ด:

class LoadAllProducts extends AsyncTask<String, String, String> 
{

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        pDialog = new ProgressDialog(CLASS.this);
        pDialog.setMessage("Loading. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) 
    {
        // Building Parameters
        doMoreStuff("internet");
        return null;
    }


    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) 
    {
         // dismiss the dialog after getting all products
         if (pDialog!=null) 
         {
                if (pDialog.isShowing()) 
                {
                    pDialog.dismiss();   //This is line 624!    
                }
         }
         something(note);
    }
}

Manifest:

    <activity
        android:name="pagename.CLASS" 
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"            
        android:label="@string/name" >
    </activity>

ฉันจะพลาดสิ่งใดเพื่อไม่ให้เกิดความผิดพลาดนี้


1
คุณเคยคิดแบบนี้ไหม? ฉันมีปัญหาเดียวกัน ดูเหมือนจะไม่สามารถคิดออก
tomjung

น่าเสียดายที่ไม่มี ฉันอาจเริ่มได้รับรางวัลเล็กน้อย คุณควรตรวจสอบหัวข้ออื่น ๆ ที่เกี่ยวข้องกับปัญหาดังกล่าวในกรณีที่พวกเขาช่วยคุณ
Howli

คุณAsyncTaskถูกประกาศภายในActivityหรือFragment?
erakitin

กรุณาโพสต์ฟังก์ชั่น "doMoreStuff ()" และ "something ()"
berserk

ปัญหาอาจเกิดจากการทำงานของเธรดหลักมากเกินไปดังนั้นให้ลองใช้ตัวจัดการเพื่อแสดงความคืบหน้าการโทรออกและหาก (pDialog! = null) บรรทัดนี้ไม่จำเป็นเพราะ isShow กำลังตรวจสอบตัวเองว่ากำลังดำเนินการอยู่หรือไม่
Madhu

คำตอบ:


446

วิธีสร้างข้อผิดพลาดซ้ำ:

  1. Settings -> Developer Options -> Don't keep Activitiesเปิดใช้งานตัวเลือกนี้บนอุปกรณ์ของคุณ:
  2. กดปุ่มโฮมขณะที่AsyncTaskกำลังดำเนินการอยู่และปุ่มProgressDialogกำลังแสดงอยู่

ระบบปฏิบัติการ Android จะทำลายกิจกรรมทันทีที่ถูกซ่อน เมื่อonPostExecuteถูกเรียกว่าActivityจะอยู่ใน"จบ"ของรัฐและจะไม่ยึดติดกับProgressDialogActivity

วิธีแก้ไข:

  1. ตรวจสอบสถานะกิจกรรมในonPostExecuteวิธีการของคุณ
  2. ยกเลิกเมธอดProgressDialogin onDestroyมิฉะนั้นandroid.view.WindowLeakedจะมีการโยนข้อยกเว้น ข้อยกเว้นนี้มักมาจากกล่องโต้ตอบที่ยังคงทำงานอยู่เมื่อกิจกรรมเสร็จสิ้น

ลองใช้รหัสคงที่นี้:

public class YourActivity extends Activity {

    private void showProgressDialog() {
        if (pDialog == null) {
            pDialog = new ProgressDialog(StartActivity.this);
            pDialog.setMessage("Loading. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
        }
        pDialog.show();
    }

    private void dismissProgressDialog() {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        dismissProgressDialog();
        super.onDestroy();
    }

    class LoadAllProducts extends AsyncTask<String, String, String> {

        // Before starting background thread Show Progress Dialog
        @Override
        protected void onPreExecute() {
            showProgressDialog();
        }

        //getting All products from url
        protected String doInBackground(String... args) {
            doMoreStuff("internet");
            return null;
        }

        // After completing background task Dismiss the progress dialog
        protected void onPostExecute(String file_url) {
            if (YourActivity.this.isDestroyed()) { // or call isFinishing() if min sdk version < 17
                return;
            }
            dismissProgressDialog();
            something(note);
        }
    }
}

9
คำตอบที่ดี! BTW, isShowing () ไม่จำเป็นเพราะเลิก () จะไม่ทำอะไรเลยถ้า isShowing () == false ซอร์สโค้ด
Peter Zhao

3
ประโยชน์ของการใช้คืออะไรisDestroyed()มากกว่าisFinishing()ใน API ทั้งหมดสำหรับวัตถุประสงค์เฉพาะนี้หรือไม่?
อเล็กซานเด Abakumov

@AlexanderAbakumov: จากสิ่งที่ฉันเข้าใจisFinishing()ไม่รับประกันว่าจะเป็นtrueถ้ากิจกรรมจะถูกทำลายโดยระบบดูเอกสารเกี่ยวกับกิจกรรม
Markus Penguin

1
@MarkusPenguin: ใช่ แต่ในกรณีนี้ถ้ามีคนพยายามใช้คำแนะนำจากความคิดเห็นของผู้เขียน// or call isFinishing() if min sdk version < 17เขาจะพบกับข้อยกเว้นเดียวกัน ดังนั้นเราจึงต้องการโซลูชันที่แตกต่างจากคำตอบนี้สำหรับแอปที่รันบน API <17.
Alexander Abakumov

@AlexanderAbakumov ฉันมีปัญหาเดียวกัน แต่การหาที่จบไม่ได้ผลสำหรับฉันมันช่วยประหยัดการบันทึก WeakReference ให้กับกิจกรรมของฉันภายใน AsyncCallback แล้ว:myActivityWeakReference.get() != null && !myActivityWeakReference.get().isFinishing()
rusito23

35

ปัญหาอาจจะว่าActivityได้รับหรือfinishedprogress of finishing

เพิ่มการตรวจสอบisFinishingและยกเลิกการโต้ตอบเฉพาะเมื่อนี่คือfalse

if (!YourActivity.this.isFinishing() && pDialog != null) {
    pDialog.dismiss();
}

isFinishing: ตรวจสอบเพื่อดูว่ากิจกรรมนี้อยู่ในขั้นตอนการจบไม่ว่าจะเป็นเพราะคุณโทรfinishเข้ามาหรือมีคนอื่นขอให้ทำเสร็จ


1
ด้วยการตรวจสอบข้อยกเว้นยังคงถูกโยน
Marcos Vasconcelos

ProgressDialog ของฉันอยู่ในชั้นเรียนซึ่งไม่ใช่กิจกรรมดังนั้นฉันจึงไม่สามารถใช้การตรวจสอบ isFinishing @Libin ได้
Maniraj

11

สำหรับการDialogสร้างใน a Fragmentฉันใช้รหัสต่อไปนี้:

ProgressDialog myDialog = new ProgressDialog(getActivity());
myDialog.setOwnerActivity(getActivity());
...
Activity activity = myDialog.getOwnerActivity();
if( activity!=null && !activity.isFinishing()) {
    myDialog.dismiss();
}

ฉันจะใช้รูปแบบนี้จะจัดการกับกรณีที่อาจจะออกจากFragmentActivity


8

ดูว่ารหัสทำงานอย่างไรที่นี่:

หลังจากเรียกใช้งาน Async งาน async จะทำงานในพื้นหลัง นั่นเป็นที่พึงปรารถนา ตอนนี้งาน Async นี้จะมีกล่องโต้ตอบความคืบหน้าซึ่งแนบมากับกิจกรรมหากคุณถามว่าจะดูรหัสได้อย่างไร:

pDialog = new ProgressDialog(CLASS.this);

คุณกำลังส่งผ่านClass.thisบริบทเป็นข้อโต้แย้ง ดังนั้นกล่องโต้ตอบความคืบหน้าจะยังคงแนบกับกิจกรรม

พิจารณาสถานการณ์สมมติ: หากเราพยายามทำกิจกรรมให้เสร็จโดยใช้วิธีเสร็จสิ้น () ขณะที่งาน async กำลังดำเนินการอยู่จุดที่คุณพยายามเข้าถึงทรัพยากรที่แนบมากับกิจกรรมคือprogress barเมื่อกิจกรรมไม่มาก ที่นั่น

ดังนั้นคุณจะได้รับ:

java.lang.IllegalArgumentException: View not attached to the window manager

ทางออกสำหรับสิ่งนี้:

1) ตรวจสอบให้แน่ใจว่ากล่องโต้ตอบถูกยกเลิกหรือยกเลิกก่อนที่กิจกรรมจะเสร็จสิ้น

2) เสร็จสิ้นกิจกรรมหลังจากที่กล่องโต้ตอบถูกยกเลิกนั่นคืองาน async จบลงแล้ว


1
เกี่ยวกับ # 2: คุณไม่สามารถควบคุมได้อย่างสมบูรณ์ว่าจะสิ้นสุดเมื่อไรActivity; Android อาจเสร็จสิ้นเมื่อใดก็ได้ ดังนั้น # 2 ไม่สมเหตุสมผล
อเล็กซานเด Abakumov

7

ตามคำตอบ @erakitin แต่ยังรองรับ Android เวอร์ชัน <API ระดับ 17 Sadly Activity.isDestroyed ()รองรับตั้งแต่ API ระดับ 17 เท่านั้นดังนั้นหากคุณกำหนดเป้าหมายระดับ API ที่เก่ากว่าเช่นเดียวกับคุณคุณจะต้อง ตรวจสอบด้วยตัวเอง ไม่ได้รับการView not attached to window managerยกเว้นหลังจากนั้น

รหัสตัวอย่าง

public class MainActivity extends Activity {
    private TestAsyncTask mAsyncTask;
    private ProgressDialog mProgressDialog;
    private boolean mIsDestroyed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (condition) {
            mAsyncTask = new TestAsyncTask();
            mAsyncTask.execute();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mAsyncTask != null && mAsyncTask.getStatus() != AsyncTask.Status.FINISHED) {
            Toast.makeText(this, "Still loading", Toast.LENGTH_LONG).show();
            return;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mIsDestroyed = true;

        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    public class TestAsyncTask extends AsyncTask<Void, Void, AsyncResult> {    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = ProgressDialog.show(MainActivity.this, "Please wait", "doing stuff..");
        }

        @Override
        protected AsyncResult doInBackground(Void... arg0) {
            // Do long running background stuff
            return null;
        }

        @Override
        protected void onPostExecute(AsyncResult result) {
            // Use MainActivity.this.isDestroyed() when targeting API level 17 or higher
            if (mIsDestroyed)// Activity not there anymore
                return;

            mProgressDialog.dismiss();
            // Handle rest onPostExecute
        }
    }
}


3

แทนที่ onConfigurationChanged และปิดกล่องโต้ตอบความคืบหน้า หากไดอะล็อกความคืบหน้าถูกสร้างขึ้นในแนวตั้งและไม่แสดงในแนวนอนก็จะโยน View ไม่ได้แนบกับข้อผิดพลาดของตัวจัดการหน้าต่าง

นอกจากนี้ยังหยุดแถบความคืบหน้าและหยุดงาน async ใน onPause (), onBackPressed และวิธี onDestroy

if(asyncTaskObj !=null && asyncTaskObj.getStatus().equals(AsyncTask.Status.RUNNING)){

    asyncTaskObj.cancel(true);

}

3

แทนที่ onDestroy ของกิจกรรมและปิดกล่องโต้ตอบของคุณและทำให้เป็นโมฆะ

protected void onDestroy ()
    {
        if(mProgressDialog != null)
            if(mProgressDialog.isShowing())
                mProgressDialog.dismiss();
        mProgressDialog= null;
    }

3

ประการแรกเหตุผลความผิดพลาดคือดัชนีของ decorView คือ -1 เราสามารถรู้ได้จากซอร์สโค้ด Android มีข้อมูลโค้ด:

ระดับ: android.view.WindowManagerGlobal

ไฟล์: WindowManagerGlobal.java

private int findViewLocked(View view, boolean required) {
        final int index = mViews.indexOf(view);
//here, view is decorView,comment by OF
        if (required && index < 0) {
            throw new IllegalArgumentException("View=" + view + " not attached to window manager");
        }
        return index;
    }

ดังนั้นเราจึงได้รับการติดตามเพียงแค่ตัดสินดัชนีของ decorView หากมีมากกว่า 0 ให้ดำเนินการต่อหรือเพียงแค่คืนค่าและยกเลิกการเลิกใช้โค้ดดังนี้:

try {
            Class<?> windowMgrGloable = Class.forName("android.view.WindowManagerGlobal");
            try {
                Method mtdGetIntance = windowMgrGloable.getDeclaredMethod("getInstance");
                mtdGetIntance.setAccessible(true);
                try {
                    Object windownGlobal = mtdGetIntance.invoke(null,null);
                    try {
                        Field mViewField = windowMgrGloable.getDeclaredField("mViews");
                        mViewField.setAccessible(true);
                        ArrayList<View> mViews = (ArrayList<View>) mViewField.get(windownGlobal);
                        int decorViewIndex = mViews.indexOf(pd.getWindow().getDecorView());
                        Log.i(TAG,"check index:"+decorViewIndex);
                        if (decorViewIndex < 0) {
                            return;
                        }
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (pd.isShowing()) {
            pd.dismiss();
        }

2

ทับdismiss()วิธีนี้:

@Override
public void dismiss() {
    Window window = getWindow();
    if (window == null) {
        return;
    }
    View decor = window.getDecorView();
    if (decor != null && decor.getParent() != null) {
        super.dismiss();
    }
}

หากต้องการทำให้เกิดปัญหาอีกครั้งให้ทำกิจกรรมให้เสร็จก่อนที่จะปิดกล่องโต้ตอบ


1

ทางออกที่ดีที่สุด ตรวจสอบบริบทแรกคือบริบทของกิจกรรมหรือบริบทของแอปพลิเคชันหากบริบทของกิจกรรมนั้นตรวจสอบเฉพาะกิจกรรมที่เสร็จสิ้นแล้วหรือไม่โทรdialog.show()หรือdialog.dismiss();

ดูตัวอย่างรหัสด้านล่าง ... หวังว่ามันจะมีประโยชน์!

แสดงกล่องโต้ตอบ

if (context instanceof Activity) {
   if (!((Activity) context).isFinishing())
     dialog.show();
}

ปิดกล่องโต้ตอบ

if (context instanceof Activity) {
       if (!((Activity) context).isFinishing())
         dialog.dismiss();
    }

หากคุณต้องการเพิ่มการตรวจสอบเพิ่มเติมจากนั้นเพิ่มdialog.isShowing()หรือdialog !-nullใช้&&เงื่อนไข


0

ปัญหานี้เกิดจากกิจกรรมของคุณเสร็จสิ้นก่อนที่จะเรียกใช้ฟังก์ชันเลิก จัดการข้อยกเว้นและตรวจสอบบันทึก ADB ของคุณด้วยเหตุผลที่แน่นอน

/**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
    try {
         if (pDialog!=null) {
            pDialog.dismiss();   //This is line 624!    
         }
    } catch (Exception e) {
        // do nothing
    }
     something(note);
}

0

ฉันมีวิธีในการทำซ้ำข้อยกเว้นนี้

ผมใช้ AsyncTask2 หนึ่งทำงานหนักและอีกงานหนึ่งทำสั้น ๆ finish()หลังจากที่งานสั้นสมบูรณ์โทร เมื่องานที่ต้องดำเนินการนานและโทรออกDialog.dismiss()จะเกิดปัญหา

นี่คือตัวอย่างรหัสของฉัน:

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate");

        new AsyncTask<Void, Void, Void>(){
            @Override
            protected void onPreExecute() {
                mProgressDialog = ProgressDialog.show(MainActivity.this, "", "plz wait...", true);
            }

            @Override
            protected Void doInBackground(Void... nothing) {
                try {
                    Log.d(TAG, "long thread doInBackground");
                    Thread.sleep(20000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                Log.d(TAG, "long thread onPostExecute");
                if (mProgressDialog != null && mProgressDialog.isShowing()) {
                    mProgressDialog.dismiss();
                    mProgressDialog = null;
                }
                Log.d(TAG, "long thread onPostExecute call dismiss");
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

        new AsyncTask<Void, Void, Void>(){
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Log.d(TAG, "short thread doInBackground");
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                Log.d(TAG, "short thread onPostExecute");
                finish();
                Log.d(TAG, "short thread onPostExecute call finish");
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
}

คุณสามารถลองสิ่งนี้และค้นหาว่าอะไรคือวิธีที่ดีที่สุดในการแก้ไขปัญหานี้ จากการศึกษาของฉันมีอย่างน้อย 4 วิธีในการแก้ไข:

  1. @ คำตอบของ erakitin: โทรisFinishing()เพื่อตรวจสอบสถานะของกิจกรรม
  2. คำตอบของ @ Kapé: ตั้งค่าสถานะเพื่อตรวจสอบสถานะของกิจกรรม
  3. ใช้ลอง / จับเพื่อจัดการ
  4. โทรในAsyncTask.cancel(false) onDestroy()มันจะป้องกันไม่ให้ asynctask ดำเนินการonPostExecute()แต่ดำเนินการonCancelled()แทน
    หมายเหตุ: onPostExecute()จะยังคงดำเนินการแม้คุณจะโทรหาAsyncTask.cancel(false)Android OS รุ่นเก่าเช่น Android 2.XX

คุณสามารถเลือกสิ่งที่ดีที่สุดสำหรับคุณ


0

อาจเป็นคุณเริ่มต้น pDialog ทั่วโลกจากนั้นลบออกและทำให้มุมมองหรือข้อความโต้ตอบภายในเครื่องมีปัญหาเดียวกันฉันได้ทำสิ่งนี้แล้วและปัญหาของฉันได้รับการแก้ไขแล้ว หวังว่ามันจะทำงานให้คุณ


0

เราได้ยกเลิกการโต้ตอบกับonPauseวิธีการหรือonDestroyวิธีการ

@Override
protected void onPause() {
    super.onPause();
    dialog.dismiss();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    dialog.dismiss();
}
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.