Search code examples
javaandroiddialogfragment

How to show a customized DialogFragment in the activity


I am trying to build an AlertDialog and want to show it when the main activity starts. But when the activity starts, an error comes up:

android.util.AndroidRuntimeException: Window feature must be requested before adding content

What I do first is creating an AlertFragment:

public class AlertFragment extends DialogFragment{

    public static AlertFragment newInstance(){
        return new AlertFragment();
    }

    public AlertFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle SavedInstanceState){

        super.onCreateView(inflater, container, SavedInstanceState);

        return inflater.inflate(R.layout.alert_dialog, container, false);
    }

    @Override
    public Dialog onCreateDialog(Bundle SaveInstanceState){

        return new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Dialog)).setMessage("Alert Dialog").setPositiveButton("Set", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getContext(), "Positive", Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getContext(), "Negative", Toast.LENGTH_SHORT).show();
            }
        }).create();
    }
}

The following is the main activity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AlertFragment AF = AlertFragment.newInstance();
        AF.show(getSupportFragmentManager(), "Dialog");
    }
}

Can someone tell me where the problem lies?


Solution

  • In your AlertFragment remove oncreateview and run

    public class AlertFragment extends DialogFragment{
    
    public static AlertFragment newInstance(){
        return new AlertFragment(); }
    
    public AlertFragment(){}
    
    
    
    @Override 
    public Dialog onCreateDialog(Bundle SaveInstanceState){
    
        return new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Dialog)).setMessage("Alert Dialog").setPositiveButton("Set", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getContext(), "Positive", Toast.LENGTH_SHORT).show();
            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getContext(), "Negative", Toast.LENGTH_SHORT).show();
            }
        }).create(); 
    } 
    
    }