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

时间:2013-04-11 11:06:01

标签: android android-activity dialog

我在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;
    }
    }
}

请帮帮我。

1 个答案:

答案 0 :(得分:3)

尝试

 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);