Android 在dialog.show() 中崩溃
我正在使用 getApplicationContext() 函数创建一个对话框,这会导致程序在调用dialog.show() 时崩溃。我正在使用 getApplicationContext() 因为我试图在 Camera.PictureCallback() 中打开对话框,如下所示:
Camera.PictureCallback pictureCallbackJpeg = new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera c)
{
Context context = getApplicationContext();
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.send_dialog);
dialog.setTitle("Send image?");
dialog.show();
camera.startPreview();
}
};
这是崩溃日志:
Thread [<1> main] (Suspended (exception WindowManager$BadTokenException))
Dialog.show() line: 245
myApp$1.onPictureTaken(byte[], Camera) line: 31
Camera$EventHandler.handleMessage(Message) line: 328
Camera$EventHandler(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 143
ActivityThread.main(String[]) line: 4914
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
有什么想法如何解决这个问题吗?
I am creating a dialog using the getApplicationContext() function and this causes the program to crash when I call dialog.show(). I am using getApplicationContext() because I am trying to make the dialog open within a Camera.PictureCallback() like this:
Camera.PictureCallback pictureCallbackJpeg = new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera c)
{
Context context = getApplicationContext();
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.send_dialog);
dialog.setTitle("Send image?");
dialog.show();
camera.startPreview();
}
};
Here is the crash log:
Thread [<1> main] (Suspended (exception WindowManager$BadTokenException))
Dialog.show() line: 245
myApp$1.onPictureTaken(byte[], Camera) line: 31
Camera$EventHandler.handleMessage(Message) line: 328
Camera$EventHandler(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 143
ActivityThread.main(String[]) line: 4914
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
Any ideas how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在
dialog.show();
上遇到异常尝试 context.dialog.show();
如果仍然存在,请检查您的上下文。
If your getting exception on
dialog.show();
try
context.dialog.show();
If persists occurs check your context also.
如果您位于活动中(例如
MyActivity
),则可以创建对话框:或者如果您位于
Activity
的内部类中:否则您可以尝试
getBaseContext()
。您只需确保您正在 UI 线程中工作即可。
If you are inside an activity (say
MyActivity
), you can create the Dialog:or if you are inside an inner class of an
Activity
:Otherwise you could try the
getBaseContext()
.You just make sure, that you're working in the UI thread.