【问题标题】:How to show a customized DialogFragment in the activity [duplicate]如何在活动中显示自定义的 DialogFragment [重复]
【发布时间】:2016-01-07 11:27:27
【问题描述】:

我正在尝试构建一个AlertDialog 并希望在主要活动开始时显示它。但是当活动开始时,出现错误:

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

我首先要做的是创建一个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();
    }
}

以下是主要活动:

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");
    }
}

谁能告诉我问题出在哪里?

【问题讨论】:

标签: java android dialogfragment


【解决方案1】:

在您的 AlertFragment 中删除 oncreateview 并运行

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(); 
} 

}

【讨论】:

    【解决方案2】:

    只需在代码中使用 show 代替 create,如下所示:

    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();
        }
    }).show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 1970-01-01
      相关资源
      最近更新 更多