2013-01-09 50 views
1

CODE對話框崩潰的Android服務

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

---編輯---

於是,我改變了代碼這一點。

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

我的應用程序仍然崩潰。這是logcat

+0

無需將活動主題更改爲對話框只需按照我所說的完成即可。 –

+0

我認爲這是不可能顯示服務的對話框,您將需要啓動一個透明的服務 –

+0

什麼是audioService?活動或服務? –

回答

2

服務活動沒有任何UI元素,所以不能顯示對話框。對話框只能從Activity上下文中添加。您可以調用具有UI的活動,如果您願意,也可以使用對話框主題,或者創建一個適用於Android警報的首選選項的通知可能會更好。

編輯

基於新的代碼,你的意圖是好的。請在應用標記中將以下內容添加到清單中:

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

我們也可以通過設置佈局來添加UI元素到對話框中。 –

+0

我們可以這樣做,是的。但是,只能在使用活動上下文時創建對話框。像服務或廣播接收機這樣的非用戶上下文將不起作用。 –

+0

他還沒有把他所有的代碼都放進去。可能他的活動名字是audioService。 –

-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) 
+0

不,還是一樣的錯誤。 –

+0

您是否添加了Multiple_task標誌? –

+0

是的,我用你的代碼替換了你的代碼片段。 –

0

使用活動上下文而不是Service的上下文。而不是對話框使用DialogFragments。