简体   繁体   中英

Android: Async Task from Runnable - “Activity has leaked window that was originally added here”

my "doInBackground" won't seem to fire when called from a runnable. Can anyone help out with my code?

This link shows the general flow of what background/runnable tasks are called. (also has my error + notes. I will also screen shot and post all below) lucidchart*com/invitations/accept/1edf5782-b8d5-4ee6-bc37-4eb3bbfe56a9

Seems the error is happening as it is trying to work on a null file, it seems the preexecute is being executed, where the file is deleted, but the doInBacground is not, where the file is rebuilt.

Seems the issue gets caught up with the dialogue.show / dismiss, which must be because the "doInBackground" isn't running.

I land on this exception in Async class when debugging: Getting this error when debugging

@MainThread
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
        Params... params) {
    if (mStatus != Status.PENDING) {
        switch (mStatus) {
            case RUNNING:
                throw new IllegalStateException("Cannot execute task:"
                        + " the task is already running.");
            case FINISHED:
                throw new IllegalStateException("Cannot execute task:"
                        + " the task has already been executed "
                        + "(a task can be executed only once)");

Error: (looks nicer from the above link)

05-30 12:59:25.955 3869-4517/com.eoinpayne.crop.cropapp E/AndroidRuntime: FATAL EXCEPTION: Thread-220
                                                                          Process: com.eoinpayne.crop.cropapp, PID: 3869
                                                                          java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                                              at libcore.net.UriCodec.encode(UriCodec.java:132)
                                                                              at java.net.URLEncoder.encode(URLEncoder.java:57)
                                                                              at com.eoinpayne.crop.cropapp.DBScraper_userVeg.run(DBScraper_userVeg.java:44)
                                                                              at java.lang.Thread.run(Thread.java:818)
05-30 12:59:26.089 3869-3877/com.eoinpayne.crop.cropapp W/art: Suspending all threads took: 70.972ms
05-30 12:59:26.113 3869-3882/com.eoinpayne.crop.cropapp I/art: Background partial concurrent mark sweep GC freed 944(142KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 1612KB/2MB, paused 2.051ms total 149.697ms
05-30 12:59:26.476 3869-3909/com.eoinpayne.crop.cropapp W/EGL_emulation: eglSurfaceAttrib not implemented
05-30 12:59:26.476 3869-3909/com.eoinpayne.crop.cropapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7fe768cb1f80, error=EGL_SUCCESS
05-30 12:59:26.686 3869-3909/com.eoinpayne.crop.cropapp W/EGL_emulation: eglSurfaceAttrib not implemented
05-30 12:59:26.686 3869-3909/com.eoinpayne.crop.cropapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7fe768e1c2c0, error=EGL_SUCCESS
05-30 12:59:27.846 3869-3869/com.eoinpayne.crop.cropapp I/Choreographer: Skipped 34 frames!  The application may be doing too much work on its main thread.
05-30 12:59:27.922 3869-3869/com.eoinpayne.crop.cropapp E/WindowManager: android.view.WindowLeaked: Activity com.eoinpayne.crop.cropapp.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{1cc88f9b V.E..... R......D 0,0-1026,422} that was originally added here
                                                                             at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
                                                                             at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
                                                                             at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
                                                                             at android.app.Dialog.show(Dialog.java:298)
                                                                             at com.eoinpayne.crop.cropapp.HomeActivity$HomeBackgroundTask.onPreExecute(HomeActivity.java:194)
                                                                             at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591)
                                                                             at android.os.AsyncTask.execute(AsyncTask.java:539)
                                                                             at com.eoinpayne.crop.cropapp.HomeActivity.onCreate(HomeActivity.java:82)
                                                                             at android.app.Activity.performCreate(Activity.java:5990)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:135)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:372)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

The structure of my calls: i.stack.imgur*com/plPmA.png

I'll edit the post with pastebins of my code I can't post more than 2 links as i don't have 10 reputation.. kind of makes this difficult

The Async task where the doInBackground doesn't work: http://pastebin.com/smtSdVmx

The code that calls it:
        try {
            BuildFile_userVeg buildFile = new BuildFile_userVeg(ctx);
            buildFile.execute(json);
        } catch (Exception e){
            e.printStackTrace();
        }

has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{1cc88f9b VE.... R......D 0,0-1026,422} that was originally added here

This exception occurs when an Activity opens Dialog and than Cos is closed without closing the dialog first.

But the real reason of your problem is here

NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

It's simply means that you are trying to get length of some object but the object is currently null. I can't tall you exact reason of the error because the piece of code that you posted is not enough

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM