2011-04-28 11 views
4

getApplicationContext()関数を使用してダイアログを作成しています。これにより、dialog.show()を呼び出すとプログラムがクラッシュします。ここでdialog.show()のAndroidクラッシュ

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] 

任意のアイデアはどのように修正するために私はこのようなCamera.PictureCallback(内オープンダイアログ)を作成しようとしているので、私はgetApplicationContext()を使用していますこの?

+0

クラッシュのログを表示できますか? – MByD

答えて

9

アクティビティ(たとえばMyActivity)の内側にある場合は、ダイアログを作成することができます

Dialog dialog = new Dialog(this); 

またはあなたがActivityの内部クラス内にある場合:それ以外の場合はあなたを

Dialog dialog = new Dialog(MyActivity.this); 

をできgetBaseContext()をお試しください。
UIスレッドで作業していることを確認してください。

+1

華麗な、ありがとう! –

-1

が持続も、あなたのコンテキストをチェックし発生した場合dialog.show(); 上のあなたの取得例外がcontext.dialog.show();

をしよう。

関連する問題