Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window feature must be requested before adding content while implementing dialog in fragment

I have a fragment in which I need to show a custom dialog.

Please check out my code below.

public class MyFragment extends Fragment{

  @Override
  public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.send_layout, container, false);
    TextView txtView = (TextView) rootView.findViewById(R.id.tv);

    txtView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openDialog();
        }
    });
   return rootView;
  }
  public void openDialog(){
      AppCompatDialog dialog = new AppCompatDialog(getContext(), R.style.package_types__dialog);
      dialog.setContentView(R.layout.package_types_dialog);
      dialog.show();
  }
}

When removing the line:

dialog.setContentView(R.layout.package_types_dialog);

there is no error, but if I use the same the following error is throwing:

FATAL EXCEPTION: main
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: Process: in.edelworks.pickedup, PID: 23866
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime: android.util.AndroidRuntimeException: Window feature must be requested before adding content
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.throwFeatureRequestIfSubDecorInstalled(AppCompatDelegateImplV7.java:1584)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.requestWindowFeature(AppCompatDelegateImplV7.java:509)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:117)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:148)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.support.v7.app.AppCompatDialog.onCreate(AppCompatDialog.java:60)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
10-20 19:42:57.489 23866-23866/in.edelworks.pickedup E/AndroidRuntime:     at android.app.Dialog.show(Dialog.java:262)
like image 666
rpn Avatar asked Oct 20 '15 14:10

rpn


1 Answers

I've implemented and tried many alternatives for your kind of situation, It's working really fine, so that I couldn't get a chance to review your error. But what I can suggest you is replace AppCompatDialog with AlertDialog.Builder which is a class of android.support.v7.app.

Replace this codes

 public void openDialog(){
      AppCompatDialog dialog = new AppCompatDialog(getContext(), R.style.package_types__dialog);
      dialog.setContentView(R.layout.package_types_dialog);
      dialog.show();
  }

with

   public void openDialog(){    
        AlertDialog.Builder dialog = new AlertDialog.Builder(getContext(),R.style.package_types__dialog);       
        dialog.setView(R.layout.package_types_dialog);   
    }

Note :

And if you have any classes that handles the dialog events then extend DialogFragment of android.support.v4.app.DialogFragment. and don't forget to override onCreateDialog method. The full class will look something like this

import android.os.Bundle;
import android.support.v4.app.DialogFragment;

/**
 * Created by Shreekrishna on 2/29/2016.
 */
public class PackageTypesDialog extends DialogFragment { 

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }
}

This will probably solve your issue !

like image 146
Shree Krishna Avatar answered Jan 02 '23 10:01

Shree Krishna