2014-09-19 2 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

Возможно, вы отправите код своей активности. –

+0

Что именно из деятельности? Это вызывается в onOptionsItemSelected –

+0

oncreate код вашей деятельности –

ответ

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 
Смежные вопросы