[TIMOB-14505] Android: NullPointerException when reusing an AlertDialog that contains an androidView
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Critical |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2013-07-19T21:24:21.000+0000 |
Affected Version/s | n/a |
Fix Version/s | 2013 Sprint 15 API, 2013 Sprint 15, Release 3.2.0 |
Components | Android |
Labels | android, module_alertdialog, qe-testadded |
Reporter | Paul Mietz Egli |
Assignee | Biju pm |
Created | 2013-06-28T16:34:34.000+0000 |
Updated | 2014-03-14T07:00:06.000+0000 |
Description
My app reuses an AlertDialog object with a custom view. The first time I call show() on the dialog, it successfully displays. When I hide it using either a cancel button or the back button, then redisplay the same dialog object, I get the following NullPointerException:
E/TiApplication( 2042): java.lang.NullPointerException
E/TiApplication( 2042): at org.appcelerator.titanium.TiBaseActivity$DialogWrapper.getActivity(TiBaseActivity.java:103)
E/TiApplication( 2042): at ti.modules.titanium.ui.widget.TiUIDialog.processView(TiUIDialog.java:165)
E/TiApplication( 2042): at ti.modules.titanium.ui.widget.TiUIDialog.processProperties(TiUIDialog.java:92)
E/TiApplication( 2042): at ti.modules.titanium.ui.widget.TiUIDialog.show(TiUIDialog.java:256)
E/TiApplication( 2042): at ti.modules.titanium.ui.AlertDialogProxy$1.run(AlertDialogProxy.java:73)
E/TiApplication( 2042): at android.os.Handler.handleCallback(Handler.java:615)
E/TiApplication( 2042): at android.os.Handler.dispatchMessage(Handler.java:92)
E/TiApplication( 2042): at android.os.Looper.loop(Looper.java:137)
E/TiApplication( 2042): at android.app.ActivityThread.main(ActivityThread.java:4745)
E/TiApplication( 2042): at java.lang.reflect.Method.invokeNative(Native Method)
E/TiApplication( 2042): at java.lang.reflect.Method.invoke(Method.java:511)
E/TiApplication( 2042): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/TiApplication( 2042): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/TiApplication( 2042): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2042): FATAL EXCEPTION: main
E/AndroidRuntime( 2042): java.lang.NullPointerException
E/AndroidRuntime( 2042): at org.appcelerator.titanium.TiBaseActivity$DialogWrapper.getActivity(TiBaseActivity.java:103)
E/AndroidRuntime( 2042): at ti.modules.titanium.ui.widget.TiUIDialog.processView(TiUIDialog.java:165)
E/AndroidRuntime( 2042): at ti.modules.titanium.ui.widget.TiUIDialog.processProperties(TiUIDialog.java:92)
E/AndroidRuntime( 2042): at ti.modules.titanium.ui.widget.TiUIDialog.show(TiUIDialog.java:256)
E/AndroidRuntime( 2042): at ti.modules.titanium.ui.AlertDialogProxy$1.run(AlertDialogProxy.java:73)
E/AndroidRuntime( 2042): at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime( 2042): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 2042): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 2042): at android.app.ActivityThread.main(ActivityThread.java:4745)
E/AndroidRuntime( 2042): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2042): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 2042): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/AndroidRuntime( 2042): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 2042): at dalvik.system.NativeStart.main(Native Method)
This appears to be happening because the TiUIDialogProxy.hide() method sets the custom view to null. The workaround is to set the androidView property on the dialog immediately before calling show() instead of setting it in the creation dict as illustrated below:
var win = Ti.UI.createWindow({
backgroundColor: 'white'
});
var view = Ti.UI.createView({
});
var alertDialog = Ti.UI.createAlertDialog({
androidView: view, // for workaround, remove this line
buttonNames: ["Ok", "Close"],
cancel: 1
});
var button = Ti.UI.createButton({
title: 'Show Dialog'
});
button.addEventListener('click', function(e) {
// alertDialog.androidView = view; // for workaround, uncomment this line
alertDialog.show();
});
win.add(button);
win.open();
Tested and confirmed on: Android GS3 4.1.2 Ti 3.1.1GA
PR:- https://github.com/appcelerator/titanium_mobile/pull/4451
AlertDialog is working fine when contains an androidView. No error occurs. Verified Fix on: Device : Google Nexus 7, Android Version: 4.3 SDK: 3.2.0.v20131018154951 CLI version : 3.2.0 OS : MAC OSX 10.8.4 Alloy : 1.2.2 Appcelerator Studio, build: 3.2.0.201310181700 XCode : 5