2014-09-19 84 views
0

我想打一個關於對話框在我的應用程序,我希望它有默認的對話框的按鈕,我決定使用DialogFragment,所以我做了這樣的DialogFragment負按鈕

public class AboutDialog extends DialogFragment { 

public AboutDialog() { 
    // Empty constructor for fragment 
} 

@Override 
@NonNull 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setTitle("About"); 
    builder.setNegativeButton("Close", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      dismiss(); 
     } 
    }); 
    return builder.create(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.about_dialog, container); 
    getDialog().setTitle(getString(R.string.action_about)); 
    return rootView; 
} 

}

然後從活動這樣

AboutDialog about = new AboutDialog(); 
     about.show(getSupportFragmentManager(), null); 

調用它,然後我得到這個錯誤

android.util.AndroidRuntimeException: requestFeature() must be called before adding content 

我該如何解決這個問題?

+0

您可以發佈您的活動代碼嗎? – 2014-09-19 09:13:22

+0

活動究竟是什麼?這在onOptionsItemSelected中被調用 – 2014-09-19 09:14:32

+0

oncreate您的活動的代碼 – 2014-09-19 09:19:38

回答

2
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // Use the Builder class for convenient dialog construction 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    builder.setView(inflater.inflate(R.layout.about_dialog, null)); 
    builder.setMessage("Test") 
      .setPositiveButton("fire", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // FIRE ZE MISSILES! 
       } 
      }) 
      .setNegativeButton("cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // User cancelled the dialog 
       } 
      }); 
    // Create the AlertDialog object and return it 
    return builder.create(); 
} 

remove your onCreateView 
相關問題