2015-04-21 86 views
1

我創建了一個對話框,無法在創建方法的主要活動中顯示它。 我喜歡這個調用對象的show無法顯示android對話框

FireMissilesDialogFragment newFragment = new FireMissilesDialogFragment(); 
newFragment.show(); 

,但我得到一個錯誤,指出它can't resolve method 'show'

這是我的對話框代碼(我正在學習如何使他們的,所以這是一個從Android開發人員網站對話框培訓複印件)

package com.shush; 

import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 


public class FireMissilesDialogFragment extends DialogFragment { 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the Builder class for convenient dialog construction 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setMessage(R.string.name) 
       .setPositiveButton(R.string.name, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // FIRE ZE MISSILES! 
        } 
       }) 
       .setNegativeButton(R.string.address, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User cancelled the dialog 
        } 
       }); 
     // Create the AlertDialog object and return it 
     return builder.create(); 
    } 
} 
+0

什麼是擴展活動類從? – Harry

回答

1

而不是

FireMissilesDialogFragment newFragment = new FireMissilesDialogFragment(); 
    newFragment.show(); 

使用:

DialogFragment newFragment = new FireMissilesDialogFragment(); 
    newFragment.show(getSupportFragmentManager(), "firemissile"); 
+0

我這樣做,但現在我得到錯誤「不兼容的類型」,並且不能解析方法顯示給定的參數 – Isaac

+1

然後,在您的MainActivity,而不是擴展'活動',擴展'FragmentActivity' – RafaelC