对话框崩溃了Android服务

时间:2013-01-09 12:27:53

标签: android service popup

CODE

         this.getApplicationContext().getContentResolver().registerContentObserver(
              android.provider.Settings.System.CONTENT_URI, 
              true, new ContentObserver(new Handler()) {
                public void onChange(boolean selfChange) {
                  Toast.makeText(audioServices.this, "Working..", Toast.LENGTH_SHORT).show();
                  //dispVC();
                  dialog = new Dialog(audioServices.this);
                    dialog.setContentView(R.layout.vc);

                    // set the custom dialog components - text, image and button
                    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                      dialog.show();
                  //System.out.println("Works!");
                }
            });

这是logcat。 注意 - 01-09 17:54:43.137: E/AndroidRuntime(7210): at com.torcellite.popupvc.audioServices$1.onChange(audioServices.java:59)

第59行是dialog.show();

--- --- EDIT

所以,我把代码改为了。

Intent dialogIntent = new Intent(audioServices.this, vcDialog.class);
                    dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    audioServices.this.startActivity(dialogIntent);

我的应用仍然崩溃。这是logcat

3 个答案:

答案 0 :(得分:2)

服务没有任何UI元素,因此无法显示对话框。只能从“活动”上下文添加对话框。您可以调用具有UI的活动,如果您愿意,可以调用对话框主题,也可以更好地创建通知,这是Android警报的首选选项。

修改

根据您的新代码,您的意图很好。而是将以下内容添加到应用程序标记中的清单中:

<activity android:name=".vcDialog" />

答案 1 :(得分:0)

使用Activity上下文而不是Service的上下文。而不是对话框使用DialogFragments。

答案 2 :(得分:-1)

dialogIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

将这些行代替

dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

线 并尝试调用这样的活动

this.getApplicationContext().startActivity(dialogIntent)