Shikhar13 / codenameone

Automatically exported from code.google.com/p/codenameone
0 stars 0 forks source link

AndroidPeer$8,$9 cause NPE in WebView when using AndroidBrowserComponent #320

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When using a WebBrowser on Android, when I restart the app on a form that has a 
WebBrowser component, I have encountered these two exceptions:

09-08 13:35:26.420: E/AndroidRuntime(5382): FATAL EXCEPTION: main
09-08 13:35:26.420: E/AndroidRuntime(5382): java.lang.NullPointerException
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
android.webkit.WebView.requestFocus(WebView.java:7650)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
android.view.View.requestFocus(View.java:5323)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
android.view.View.requestFocusFromTouch(View.java:5401)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
com.codename1.impl.android.AndroidImplementation$AndroidPeer$8.run(Unknown 
Source)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
android.os.Handler.handleCallback(Handler.java:605)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
android.os.Handler.dispatchMessage(Handler.java:92)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
android.os.Looper.loop(Looper.java:137)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
android.app.ActivityThread.main(ActivityThread.java:4424)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
java.lang.reflect.Method.invokeNative(Native Method)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
java.lang.reflect.Method.invoke(Method.java:511)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-08 13:35:26.420: E/AndroidRuntime(5382):     at 
dalvik.system.NativeStart.main(Native Method)

09-08 13:04:12.381: E/AndroidRuntime(4638): FATAL EXCEPTION: main
09-08 13:04:12.381: E/AndroidRuntime(4638): java.lang.NullPointerException
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
android.webkit.WebView.requestFocus(WebView.java:7650)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2154)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
android.view.ViewGroup.requestFocus(ViewGroup.java:2113)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
android.view.View.requestFocus(View.java:5323)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
android.view.View.requestFocus(View.java:5301)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
com.codename1.impl.android.AndroidImplementation$AndroidPeer$9.run(Unknown 
Source)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
android.os.Handler.handleCallback(Handler.java:605)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
android.os.Handler.dispatchMessage(Handler.java:92)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
android.os.Looper.loop(Looper.java:137)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
android.app.ActivityThread.main(ActivityThread.java:4424)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
java.lang.reflect.Method.invokeNative(Native Method)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
java.lang.reflect.Method.invoke(Method.java:511)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-08 13:04:12.381: E/AndroidRuntime(4638):     at 
dalvik.system.NativeStart.main(Native Method)

I believe that these NPE's are happening because 
AndroidBrowserComponent.deinitialize calls destroy() on the WebView, at which 
point no other methods may be called on the WebView. When the app goes to the 
background, AndroidBrowserComponent is deinitialized; therefore, when the app 
restarts the underlying WebView is no longer valid causing the NPE.

A better way would be to have a destroy() (or similarly named method) on the 
WebBrowser component so that the developer can destroy the component manually 
when it is known that it is no longer needed.

Original issue reported on code.google.com by Ian.Te...@gmail.com on 8 Sep 2012 at 8:32

GoogleCodeExporter commented 9 years ago
Update after further testing, I have been able to get these npe's without 
restarting my app (meaning I never left the app after initially starting it). 
In several of the instances where I got these exceptions, right before I had a 
bunch of the following NPE one after the other finally resulting in one of the 
exceptions above.

09-08 21:20:32.111: E/Codename One(14453): Err on EDT
09-08 21:20:32.111: E/Codename One(14453): java.lang.NullPointerException
09-08 21:20:32.111: E/Codename One(14453):  at 
com.codename1.ui.Component.pointerDragged(Unknown Source)
09-08 21:20:32.111: E/Codename One(14453):  at 
com.codename1.ui.Form.pointerDragged(Unknown Source)
09-08 21:20:32.111: E/Codename One(14453):  at 
com.codename1.ui.Component.pointerDragged(Unknown Source)
09-08 21:20:32.111: E/Codename One(14453):  at 
com.codename1.ui.Display.handleEvent(Unknown Source)
09-08 21:20:32.111: E/Codename One(14453):  at 
com.codename1.ui.Display.edtLoopImpl(Unknown Source)
09-08 21:20:32.111: E/Codename One(14453):  at 
com.codename1.ui.Display.mainEDTLoop(Unknown Source)
09-08 21:20:32.111: E/Codename One(14453):  at 
com.codename1.ui.RunnableWrapper.run(Unknown Source)
09-08 21:20:32.111: E/Codename One(14453):  at 
com.codename1.impl.CodenameOneThread.run(Unknown Source)

Original comment by Ian.Te...@gmail.com on 9 Sep 2012 at 2:27

GoogleCodeExporter commented 9 years ago
I have been able to figure out exactly how to reproduce this exception. I have 
Container with an empty Label as its only Component in CENTER position of a 
BorderLayout. I them call Container.replace(getComponentAt(0), 
myBrowserComponent, null). Before and during the replace, I make click and drag 
motions on the screen in the container that the replace is taking place in. I 
then get a lot of the NPE's in com.codename1.ui.Component.pointerDragged, then 
finally the npe in android.webkit.WebView.requestFocus resulting in my app 
crashing.

Original comment by Ian.Te...@gmail.com on 9 Sep 2012 at 3:34

GoogleCodeExporter commented 9 years ago
Assigning to Chen

Original comment by shai.almog on 9 Sep 2012 at 4:33

GoogleCodeExporter commented 9 years ago
Correction to my previous post, the Container.replace call uses a transition, 
so it looks like this:
replace(getComponentAt(0), subclassOfWebBrowser, 
UIManager.getInstance().getLookAndFeel().getDefaultFormTransitionOut());

Also, although I did not leave the app, I clicked on an ad inside of the 
WebBrowser causing a url to executed using Display.execute, I hit back when the 
"Complete action using" Android dialog appeared which caused the current form 
with the WebBrowser to be denintialized and then initialized again. So it still 
may be an issue with calling destroy on the WebView in 
AndroidBrowserComponent.deinitialize.

Original comment by Ian.Te...@gmail.com on 9 Sep 2012 at 3:00

GoogleCodeExporter commented 9 years ago
Forgot to actually asign

Original comment by shai.almog on 9 Sep 2012 at 4:28

GoogleCodeExporter commented 9 years ago
Another update :)
The exception happens mostly when the the component being replaced (component 
returned by getComponentAt(0)) is another WebBrowser, so I'm basically 
replacing one WebBrowser with another.

I was able to work around this issue by not using Container.replace, but 
instead forgetting about the transition and just calling removeAll and 
addComponent manually on the Container. With this change have been unable to 
reproduce the exception.

So my guess that there is something wrong in the Container.replace 
transition/animation that allows pointer dragged events on the component being 
removed - which in my case was a WebBrowser that is no longer valid.

Hope this helps and sorry about all of these updates.

Original comment by Ian.Te...@gmail.com on 9 Sep 2012 at 10:22

GoogleCodeExporter commented 9 years ago
added destory to the WebBrowser and removed the deinitalize from the peer 
component.
I still would not recommend doing transitions with peer components

Original comment by cf27...@gmail.com on 10 Sep 2012 at 9:20

GoogleCodeExporter commented 9 years ago

Original comment by cf27...@gmail.com on 10 Sep 2012 at 9:21

GoogleCodeExporter commented 9 years ago
I think there are some errors in the last 2 revisions that you committed to fix 
this.

First in r463, AndroidImplementation.browserDestroy calls stop() on the 
AndroidBrowserComponent not destroy().

In r464, you did not add the destroy() method to the BrowserComponent class 
that calls CodenameOneImplementation.browserDestroy, so WebBrowser.destroy 
should be causing an error right now.

Original comment by Ian.Te...@gmail.com on 10 Sep 2012 at 12:57

GoogleCodeExporter commented 9 years ago
thanks, fixed the issues

Original comment by cf27...@gmail.com on 10 Sep 2012 at 1:08