简体   繁体   中英

Android app crashes on loading during loading when touched

I made an 3d app (min3D framework) which runs inside a layout. The layout contains some buttons to control the game. This works fine. But the layout appears faster than the game itself and when I touch the area where the app is loading it will crash. If I wait and don't touch the area I get no errors. Everything works fine then.

How can I fix this?

I now know that the app is crashing on the point where it should add an object which isn't loaded yet. Its all in the onTouchEvent function. But why is this working before the initScene has finished?

logCat:

01-13 21:17:20.476: E/AndroidRuntime(20189): FATAL EXCEPTION: main
01-13 21:17:20.476: E/AndroidRuntime(20189): java.lang.NullPointerException
01-13 21:17:20.476: E/AndroidRuntime(20189):    at com.appboss.min3d.core.Scene.addChild(Scene.java:112)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at com.appboss.soyuz.Rocket.onTouchEvent(Rocket.java:250)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at android.app.Activity.dispatchTouchEvent(Activity.java:2231)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1795)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2336)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1976)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at android.os.Looper.loop(Looper.java:150)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at android.app.ActivityThread.main(ActivityThread.java:4277)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at java.lang.reflect.Method.invokeNative(Native Method)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at java.lang.reflect.Method.invoke(Method.java:507)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-13 21:17:20.476: E/AndroidRuntime(20189):    at dalvik.system.NativeStart.main(Native Method

You should only enable/add the touch listener once the objects have been loaded. Util that time perhaps show a loading dialog/waiting screen. Your logcat explains its a NullPointerException

Scene.java, line 112 in the addChild() method

com.appboss.soyuz.Rocket.onTouchEvent(Rocket.java:250) this is your culprit. You can catch it before the app is fully loaded - make a flag isCreated(isLoaded) or else. Do you have a loading screen before giving a control to a user?

Why not try and use AsyncTask to load the game in the background and pop up a "Please wait while game loads" dialog that will get dismissed at the end of the doInBackground() of Asynctask?

That's the best workaround I know of, but it assumes you don't mind a small loading dialog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM