我的应用程序遇到问题,当电话被电话中断或按下主页键时,它会强制关闭。当用户返回应用程序时,他们会收到强制关闭消息。我已阅读以下问题并尝试回答建议:Force Close when opening app after pressing home button和此处:Android crash when app is closed and reopened。 在第二个问题中提出的答案没有帮助,当我添加wait()时,它在应用程序打开时给了我一个强制关闭,当我添加notify()时,它给了我一个力,当我离开应用程序时关闭。我没有想法,现在正在寻找,所以看看是否有人可能有这个问题的解决方案。
我正在使用surfaceCreated& amp; LunarLander使用的surfaceDestroyed方法。
先谢谢。
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方法:
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方法:
public void surfaceCreated(SurfaceHolder holder) {
thread.initLevel();
thread.setRunning(true);
thread.start();
//thread.notify();
}
编辑:
在哪里我尝试切换与通知,等待。我也尝试在尝试之前放置通知并将等待放在连接位置。
答案 0 :(得分:2)
强制关闭的应用程序始终会生成可在logcat缓冲区中看到的堆栈跟踪。如果您的应用程序强制关闭,堆栈跟踪的顶部将包含应用程序内的一个类,标记触发崩溃的行号。
调查该行号以查看出现了什么问题。 (有关更详细的答案,您还可以通过更新问题来提供更多详细信息,以包含导致崩溃的堆栈跟踪+代码。)
使用适当的异常处理&amp;编码,应用程序应该很少崩溃(除非android子系统关闭你的应用程序)。
在您的情况下,您有几个线程问题。
你不能两次启动同一个线程,并且在线程上调用wait之前你不能调用notify。在同步的方法/块中调用wait(),notify(),notifyAll()也是一个好习惯。 (有关详细信息,请参阅此处:http://www.xyzws.com/Javafaq/why-wait-notify-notifyall-must-be-called-inside-a-synchronized-method-block/127)
我还建议在Java中阅读并发性:http://www.javaconcurrencyinpractice.com/