Android Question Is there a requirement to load layout first?

Computersmith64

Well-Known Member
Licensed User
Longtime User
Since I implemented some error tracking & reporting in my Yahtzee app, I'm getting some "java.lang.NullPointerException" reports back. The stack trace looks like this:

anywheresoftware.b4a.agraham.threading.Threading$ExceptionWrapper.Initialize(Threading.java:380)
com.airlinemates.yahtzee.main._activity_create(main.java:458)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
anywheresoftware.b4a.BA.raiseEvent2(BA.java:173)
com.airlinemates.yahtzee.main.afterFirstLayout(main.java:98)
com.airlinemates.yahtzee.main.access$100(main.java:16)
com.airlinemates.yahtzee.main$WaitForLayout.run(main.java:76)
android.os.Handler.handleCallback(Handler.java:725)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:5293)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
dalvik.system.NativeStart.main(Native Method)

If I look through my main.java file, the lines referred to in the trace are:

com.airlinemates.yahtzee.main$WaitForLayout.run(main.java:76)
B4X:
mostCurrent.afterFirstLayout();

com.airlinemates.yahtzee.main.access$100(main.java:16)
B4X:
public class main extends Activity implements B4AActivity{

com.airlinemates.yahtzee.main.afterFirstLayout(main.java:98)
B4X:
processBA.raiseEvent2(null, true, "activity_create", false, isFirst);

com.airlinemates.yahtzee.main._activity_create(main.java:458)
B4X:
mostCurrent._vvvvvvvvvvvvvvvvvvvv6.Initialize("");
(the code is obfuscated, but this is the initialization for the ExceptionEx object)

I can't see anything here that should be causing a null pointer exception, however one thing I did notice is that over time, my activity.loadlayout() call has ended up a few lines down in the Activity_Create code. Is there a requirement that the loadlayout be called first? There is nothing (that I can see) in the code that comes before the loadlayout call that relies on anything in the layout, so I don't think that's the issue. The only thing I just noticed is that the layout is saved as "yahtzee-small", but the loadlayout uses "Yahtzee-small" for the layout name. Is it case sensitive on some devices? It hasn't been an issue on any of my devices & I'm not getting thousands of reports of this error (there are about 40K current installs of the app) - so I'm wondering if perhaps it's device specific.

Thanks - Colin.
 

DonManfred

Expert
Licensed User
Longtime User
Without to see any code we just can guess what the problem is...
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Without to see any code we just can guess what the problem is...

Well really the questions are:

"Is there a requirement to load the layout before any other code is executed?"; &
"Is the layout name case sensitive for some versions of Android?"

I don't think you need to see the code to answer those questions, but thanks anyway.

- Colin.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Requirement depends on what you are doing. You should not use "Objects from Layout" before you have loaded the layout. Cause the objects from Layout are created and initialized when the Layout is loaded.

For example. You have a listview in your layout. In create you want to add items to this listview before the layout is loaded. You´ll get an nullpointer-exception.

First load the Layout and then add Items or change properties (button1.Visible = false) or values (edittext1.Text = "new") to Objects from this layout.

I don´t know whether the layoutname is case sensitive for some versions of Android or not.
I suggest to use only characters and numbers and all in lowercase and with not _-(/ or whatever (myown1, viewcart, and so on). You are on the saver side then even if the case are a problem on some android versions.
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
No.

The error suggests that the error is actually in the ExceptionEx initialize method:
No.

The error suggests that the error is actually in the ExceptionEx initialize method:

Thanks Erel - I thought the same thing, but then I thought I must be reading the trace wrong because I was seeing the same null pointer exception through LastException, before I started using ExceptionEx. Of course I didn't have a stack trace in that case so I couldn't see any more detail on where it was coming from.

Notice that I initialize ExceptionEx with an empty string - is it OK to do this? I'm assuming yes, because I'm not seeing this error consistently. A timing issue somewhere perhaps? The initialization of ExceptionEx is the very first line of code in my Activity_Create.

Thanks - Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Atually, I think this -> https://code.google.com/p/android/issues/detail?id=8886 might be the cause of the exception I'm seeing, because one of the first calls in Activity_Create is to initialize a class I use for handling game stats & the initialization involves loading the stats from a file in DirInternal. I've put some code in to catch the exception & retry the file read (as suggested in the Google support thread), so once I release this version, I'll see if it stops the exception reports.

- Colin.
 
Upvote 0
Top