ricardoalcocer / actionbarextras

Titanium Android Native Module that exposes ActionBar features not exposed by the Titanium SDK
MIT License
149 stars 60 forks source link

Java NullPointer when going "back" twice and trying to set HomeAsUpIcon #88

Closed molsen-ee closed 8 years ago

molsen-ee commented 8 years ago

If a window has a 'focus' event that tries to call the abx.setHomeAsUpIcon() function there will be a Java NullPointer exception

Steps to reproduce: 1) Create a window that uses actionbarextras (abx) 2) add an eventListener('focus') that calls abx.setHomeAsUpIcon() 3) Have functionality that allows this window to be opened multiple times (such as a label with an onClick event) 4) After opening this window multiple times, press the Android "back" button at least twice 5) The second "back", when calling the 'focus' event, will throw the following exception (stack trace truncated):

[ERROR] :  TiApplication: (main) [4329,18423] Sending event: exception on thread: main msg:java.lang.NullPointerException; Titanium 4.1.1,2015/08/16 21:42,7e39876
[ERROR] :  TiApplication: java.lang.NullPointerException
[ERROR] :  TiApplication:   at com.alcoapps.actionbarextras.ActionbarextrasModule.getActionBar(ActionbarextrasModule.java:126)
[ERROR] :  TiApplication:   at com.alcoapps.actionbarextras.ActionbarextrasModule.handleSetHomeAsUpIcon(ActionbarextrasModule.java:538)
[ERROR] :  TiApplication:   at com.alcoapps.actionbarextras.ActionbarextrasModule.handleMessage(ActionbarextrasModule.java:178)
[ERROR] :  TiApplication:   at android.os.Handler.dispatchMessage(Handler.java:95)
manumaticx commented 8 years ago

Hi @molsen-ee Thanks for reporting this. I'll test this asap. Does this happen with the latest version of the module?

molsen-ee commented 8 years ago

Yes, I was able to reproduce this bug with version 1.6.7 of the module.

manumaticx commented 8 years ago

Have you tried using the window property?

molsen-ee commented 8 years ago

The window property of the abx object? I don't follow what you are asking.

manumaticx commented 8 years ago

Sorry, I'm not on a computer right now. But yes, abx has a window property which allows you to define a window reference. When you open multiple instances of a window, it could be a good idea to make use of this property. That way you can make sure that abx uses the actionbar according to the correct window. By default it uses the current window which is open. This could lead into timing issues. If it references a window that is not yet open or not open anymore the actionbar reference may be null. Thus there occur a NullPointerException. This is indeed just a guess but I'll test this tomorrow and let you know. I think there is a simple way to fix this.

molsen-ee commented 8 years ago

Ah hah! That gave me just enough insight to get around the timing issue. In the controller logic I was creating the abx javascript variable via requre and setting the abx.window property. However in the focus event handler I was not setting the abx.window property to the current window so at the Java level it was still referencing a window that had been opened later in the logic flow.

Simplified version of my code:

var abx = require('com.alcoapps.actionbarextras');
abx.window = $.thisWindow;

$.thisWindow.addEventListener('focus', function(e) {
   abx.window = $.thisWindow;  // adding this line solved the problem!

   if (useDifferentIcon())
      abx.setHomeAsUpIcon("/images/differentIcon.png");
   else
      abx.setHomeAsUpIcon("/images/defaultIcon.png");
});
manumaticx commented 8 years ago

Hm, this NullPointerException should get catched here

manumaticx commented 8 years ago

@molsen-ee Can we close this issue?

molsen-ee commented 8 years ago

I was confused about the stack trace's line numbering as well. Is the 1.6.7 jar built on the current code?

Yes, the change in logic is working for me. Thank you for the help.