Reputation: 137
I am having issues with my app where it force closes when ever it is interrupted by a phone call or the home key is pressed. When the user goes back to the app it they receive the force close message. I have read the following question and tried to answer suggested: Force Close when opening app after pressing home button and here: Android crash when app is closed and reopened. The answers posed in the second question did not help and when I added wait()it gave me a force close when the app opened and when I added notify() it gave me a force closed when ever I left the app. I am out of ideas and now looking for so see if anyone might have a solution to this problem.
I am using a surfaceview with the surfaceCreated & surfaceDestroyed methods used by LunarLander.
Thanks in Advance.
LogCat:
Log1:<br>
07-08 02:18:32.284: ERROR/AndroidRuntime(627): Uncaught handler: thread main exiting due to uncaught exception
07-08 02:18:32.354: ERROR/AndroidRuntime(627): java.lang.IllegalThreadStateException: Thread already started.
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at java.lang.Thread.start(Thread.java:1322)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at com.android.hitmanassault.HitmanView.surfaceCreated(HitmanView.java:115)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at android.view.SurfaceView.updateWindow(SurfaceView.java:454)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:189)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at android.view.View.dispatchWindowVisibilityChanged(View.java:3782)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:692)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:692)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at android.view.ViewRoot.performTraversals(ViewRoot.java:706)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at android.os.Looper.loop(Looper.java:123)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at java.lang.reflect.Method.invoke(Method.java:521)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-08 02:18:32.354: ERROR/AndroidRuntime(627): at dalvik.system.NativeStart.main(Native Method)
Log2:
07-08 02:21:39.805: ERROR/AndroidRuntime(663): Uncaught handler: thread main exiting due to uncaught exception
07-08 02:21:39.854: ERROR/AndroidRuntime(663): java.lang.IllegalMonitorStateException: object not locked by thread before notify()
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at java.lang.Object.notify(Native Method)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at com.android.hitmanassault.HitmanView.surfaceDestroyed(HitmanView.java:135)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at android.view.SurfaceView.reportSurfaceDestroyed(SurfaceView.java:488)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at android.view.SurfaceView.updateWindow(SurfaceView.java:413)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:189)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at android.view.View.dispatchWindowVisibilityChanged(View.java:3782)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:692)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:692)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at android.view.ViewRoot.performTraversals(ViewRoot.java:706)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at android.os.Looper.loop(Looper.java:123)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at java.lang.reflect.Method.invoke(Method.java:521)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-08 02:21:39.854: ERROR/AndroidRuntime(663): at dalvik.system.NativeStart.main(Native Method)
SurfaceDestroyed Methods:
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, "Surface is being destroyed");
boolean retry = true;
thread.setRunning(false);
thread.notify();
while (retry) {
try {
thread.join();
//thead.notify(); ---I would get rid of the join part
retry = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log.d(TAG, "Thread was shut down cleanly");
}
SurfaceCreated Method:
public void surfaceCreated(SurfaceHolder holder) {
thread.initLevel();
thread.setRunning(true);
thread.start();
//thread.notify();
}
EDIT:
where I have the join at tried switching with notify, wait. I also tried putting notify before the try and putting wait where join is.
Upvotes: 0
Views: 499
Reputation: 22603
An app that force-closes always produces a stack trace that can be seen in your logcat buffer. If your app force-closes, the top of the stacktrace will contain one of the classes inside your app, marking the line-number that triggered the crash.
Investigate that line-number to see what went wrong. (For a more detailed answer, you can also provide more details by updatingg your question to include the stacktrace + code that leads up to the crash.)
By using proper exception handling & coding, an application should rarely crash (unless the android subsystem brings down your app).
In your case, you have several threading issues.
You cannot start the same thread twice, and you cannot call notify before calling a wait on the thread. It's also a good practice to call wait(), notify(), notifyAll() must in a synchronized method/block. (See this for more info : http://www.xyzws.com/Javafaq/why-wait-notify-notifyall-must-be-called-inside-a-synchronized-method-block/127)
I would also suggest doing some reading on concurrency in Java : http://www.javaconcurrencyinpractice.com/
Upvotes: 2