onActivityResult中的自定义对话框显示错误

问题描述:

我在onActivityResult中有一个自定义Dialoge。但这样表示onActivityResult中的自定义对话框显示错误

04-11 16:40:32.141: D/dalvikvm(8897): GC_EXTERNAL_ALLOC freed 689 objects/53336 bytes in 100ms 
04-11 16:40:43.901: D/UPC Code(8897): 000014312001220 
04-11 16:40:44.001: D/AndroidRuntime(8897): Shutting down VM 
04-11 16:40:44.001: W/dalvikvm(8897): threadid=1: thread exiting with uncaught exception (group=0x40028a00) 
04-11 16:40:44.041: E/AndroidRuntime(8897): FATAL EXCEPTION: main 
04-11 16:40:44.041: E/AndroidRuntime(8897): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=195543262, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {com.heuristics.barcodescanner/com.heuristics.barcodescanner.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3712) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3754) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.app.ActivityThread.access$2800(ActivityThread.java:135) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2162) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.os.Looper.loop(Looper.java:143) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.app.ActivityThread.main(ActivityThread.java:4914) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at java.lang.reflect.Method.invoke(Method.java:521) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at dalvik.system.NativeStart.main(Native Method) 
04-11 16:40:44.041: E/AndroidRuntime(8897): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.view.ViewRoot.setView(ViewRoot.java:513) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.app.Dialog.show(Dialog.java:241) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at com.heuristics.barcodescanner.MainActivity.onActivityResult(MainActivity.java:137) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.app.Activity.dispatchActivityResult(Activity.java:3931) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3708) 
04-11 16:40:44.041: E/AndroidRuntime(8897):  ... 11 more 
04-11 16:40:46.191: I/Process(8897): Sending signal. PID: 8897 SIG: 9 

和我onActivityResult错误是这样

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(requestCode) { 
    case IntentIntegrator.REQUEST_CODE: { 
     if (resultCode != RESULT_CANCELED) { 
      IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 
      if (scanResult != null) { 
       String upc = scanResult.getContents(); 

       Log.d("UPC Code",upc); 
       Product_list.add(upc); 


       final Dialog dialog = new Dialog(getApplicationContext(), android.R.style.Theme_Translucent_NoTitleBar); 
       dialog.setCancelable(false); 
       dialog.setContentView(R.layout.quantity); 
       Toast.makeText(getApplicationContext(), "DIALOGUE", Toast.LENGTH_SHORT).show(); 

       final Button navi_left; 
       Button navi_right, reset, ok; 
       final TextView quantity_text; 


       navi_left = (Button)dialog.findViewById(R.id.navi_left); 
       navi_right = (Button)dialog.findViewById(R.id.navi_right); 
       reset = (Button)dialog.findViewById(R.id.reset); 
       ok = (Button)dialog.findViewById(R.id.ok); 
       quantity_text = (TextView)dialog.findViewById(R.id.quantity_nums); 




       navi_left.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         String pre_= quantity_text.getText().toString(); 
         int post = Integer.parseInt(pre_); 
         int post_value; 
         if(post<=0) 
         { 

          post_value = post; 
         } 
         else 
         { 
          post_value = post-1; 
         } 

        } 
       }); 

       navi_right.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         String pre_= quantity_text.getText().toString(); 
         int post_ = Integer.parseInt(pre_)+1; 
         quantity_text.setText(post_+""); 

        } 
       }); 

       reset.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         quantity_text.setText(0); 

        } 
       }); 

       ok.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         dialog.dismiss(); 
         Quantity_list.add(quantity_text.getText().toString()); 
        } 
       }); 
       dialog.show(); 


       ArrayAdapter<String> my_adapter = new Custom_Adapter(getApplicationContext(), Product_list); 
       my_adapter.notifyDataSetChanged(); 
       my_list.setAdapter(my_adapter); 


      } 
     } 
     break; 
    } 
    } 
} 

请帮助我。

尝试

navi_left = (Button)dialog.findViewById(R.id.navi_left); 

执行相同的所有按钮和TextView的

相反getApplicationContext的()试试您的快速反应MainActivity.this

final Dialog dialog = new Dialog(MainActivity.this, android.R.style.Theme_Translucent_NoTitleBar);   
+0

谢谢,但问题是没有尚未解决。请看问题。我现在就编辑它。 – 2013-04-11 11:18:13

+0

检查更新并尝试 – Raghunandan 2013-04-11 11:29:01

+1

谢谢你,我的问题解决了。请你解释一下为什么getApplicationContext()显示错误? – 2013-04-11 11:32:32