Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException: 'void android.app.Activity.runOnUiThread(java.lang.Runnable)' on a null object reference #1002

Closed
3 tasks done
secondVISION opened this issue Jun 19, 2020 · 5 comments · Fixed by #1148
Labels
info-needed / awaiting response Further information is requested

Comments

@secondVISION
Copy link
Contributor

secondVISION commented Jun 19, 2020

Bug Report

Problem

What is expected to happen?

Cordova should silently exit.

What does actually happen?

Cordova crashes because the parent Activity is not alive anymore

Information

Start a CordovaWebview and load a slow/large website.

cordovaWebView.loadUrl(url)

If you return to the previous activity (by using the (hardware) back button), cordova will try to execute cordova.getActivity().runOnUiThread(), but the activity is no longer there.

e.g., https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaWebViewImpl.java#L187

Stacktrace:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.runOnUiThread(java.lang.Runnable)' on a null object reference
        at org.apache.cordova.CordovaWebViewImpl$2.run(CordovaWebViewImpl.java:179)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)

Command or Code

See information above on how to reproduce.

Environment, Platform, Device

Android / All devices / All OS

Version information

Android Studio
Cordova 8.1.0

Checklist

  • I searched for existing GitHub issues:
    Found issue: Application crash in CordovaWebViewImpl.java runOnUiThread #884 but this was closed due to lack of information
  • I updated all Cordova tooling to most recent version:
    Crash occured on
    'org.apache.cordova:cordova:6.2.3'
    Updated to (crashed as well):
    'org.apache.cordova:framework:8.1.0'
  • I included all the necessary information above
@jcesarmobile
Copy link
Member

can you provide a sample app?

@jcesarmobile jcesarmobile added the info-needed / awaiting response Further information is requested label Jun 24, 2020
@jaeheonshim
Copy link

jaeheonshim commented Jun 26, 2020

You should be able to replicate the problem by enabling the "Don't keep activities" option in Android Developer Options (Developer options => Apps => Don't keep activities (Destroy every activity as soon as the user leaves it.))

@breautek
Copy link
Contributor

When doing that on the android simulator, I run into a different crash stacktrace

D/CordovaActivity: Paused the activity.
D/CordovaActivity: Stopped the activity.
D/CordovaActivity: CordovaActivity.onDestroy()
D/CordovaWebViewImpl: >>> loadUrl(about:blank)
W/cr_AwContents: WebView.destroy() called while WebView is still attached to window.
W/cr_AwContents: Application attempted to call on a destroyed WebView
    java.lang.Throwable
        at org.chromium.android_webview.AwContents.t(chromium-TrichromeWebViewGoogle.apk-stable-410404481:2)
        at Mt.loadingStateChanged(chromium-TrichromeWebViewGoogle.apk-stable-410404481:2)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:335)
        at android.os.Looper.loop(Looper.java:183)
        at android.app.ActivityThread.main(ActivityThread.java:7523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
E/chromium: [ERROR:aw_browser_terminator.cc(125)] Renderer process (5916) crash detected (code -1).

Maybe this depends on the API level of the device and/or the installed chrome version though. It kinda seems related.

@secondVISION
Copy link
Contributor Author

Created a sample app: https://github.com/secondVISION/cordova-android-crash

How to reproduce:

  • Click on "Start Cordova"
  • Immediately press the Back key
  • Wait on crash. Cordova will try to load the website in an activity which has already been destroyed.
    • Waiting can take up to 30 seconds I've noticed

Crash log:

2020-07-16 14:06:20.618 19256-19392/com.example.cordovacrash D/EGL_emulation: eglMakeCurrent: 0x8c152aa0: ver 3 0 (tinfo 0x8859b8c0)
2020-07-16 14:06:39.652 19256-19383/com.example.cordovacrash E/AndroidRuntime: FATAL EXCEPTION: pool-2-thread-1
    Process: com.example.cordovacrash, PID: 19256
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Activity.runOnUiThread(java.lang.Runnable)' on a null object reference
        at org.apache.cordova.CordovaWebViewImpl$2.run(CordovaWebViewImpl.java:181)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
2020-07-16 14:06:39.819 19256-19310/com.example.cordovacrash D/EGL_emulation: eglMakeCurrent: 0xa1305300: ver 3 0 (tinfo 0xa13031f0)

@secondVISION
Copy link
Contributor Author

cordovacrashgif

secondVISION pushed a commit to secondVISION/cordova-android that referenced this issue Aug 12, 2020
secondVISION pushed a commit to secondVISION/cordova-android that referenced this issue Aug 12, 2020
secondVISION pushed a commit to secondVISION/cordova-android that referenced this issue Jan 18, 2021
secondVISION pushed a commit to secondVISION/cordova-android that referenced this issue Jan 18, 2021
breautek pushed a commit that referenced this issue Mar 27, 2021
)

* (android) #1002: Add Null Pointer Checks to prevent Cordova from running on a destroyed activity

* (android) Add logging statements if Cordova Activity does not exist anymore (i.e. is destroyed)

Co-authored-by: Habets Rick <rick.habets@kbc.be>
wedgberto pushed a commit to wedgberto/cordova-android that referenced this issue May 17, 2022
…ache#1148)

* (android) apache#1002: Add Null Pointer Checks to prevent Cordova from running on a destroyed activity

* (android) Add logging statements if Cordova Activity does not exist anymore (i.e. is destroyed)

Co-authored-by: Habets Rick <rick.habets@kbc.be>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info-needed / awaiting response Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants