android – 从Fragment调用DialogFragment

android – 从Fragment调用DialogFragment,第1张

概述我试图从我的Fragment类调用DialogFragment.我有一个 ImageView,并希望在我设置的ImageView的onClickListener中调用我的DialogFragment类. 我在onClick中遇到错误,我设置的代码试图调用DialogFragment. 我在“show”上收到一条错误,说明“DialogFragment类型中的方法show(FragmentManag 我试图从我的Fragment类调用DialogFragment.我有一个 ImageVIEw,并希望在我设置的ImageVIEw的onClickListener中调用我的DialogFragment类.

我在onClick中遇到错误,我设置的代码试图调用DialogFragment.

我在“show”上收到一条错误,说明“DialogFragment类型中的方法show(FragmentManager,String)不适用于参数(FragmentManager,String)”,并且“new Instance”上的错误表明“方法newInstance()未定义MyDialogFragment类型“

这是我的代码:

@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState){    final VIEw v = inflater.inflate(R.layout.image_detail_fragment,container,false);    mImageVIEw = (RecyclingImageVIEw) v.findVIEwByID(R.ID.imageVIEw);    mImageVIEw.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw arg0) {            //Here            MyDialogFragment dialog = MyDialogFragment.newInstance();            dialog.show(getFragmentManager(),"fragmentDialog");        }    });    return v;}

DialogFragment类:

import androID.app.AlertDialog;import androID.app.Dialog;import androID.app.DialogFragment;import androID.content.Context;import androID.content.DialogInterface;import androID.os.Bundle;class MyDialogFragment extends DialogFragment {    Context mContext;    public MyDialogFragment() {        mContext = getActivity();    }    @OverrIDe    public Dialog onCreateDialog(Bundle savedInstanceState) {        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);        alertDialogBuilder.setTitle("Set Wallpaper?");        alertDialogBuilder.setMessage("Are you sure?");        //null should be your on click Listener        alertDialogBuilder.setPositivebutton("OK",null);        alertDialogBuilder.setNegativebutton("Cancel",new DialogInterface.OnClickListener() {            @OverrIDe            public voID onClick(DialogInterface dialog,int which) {              dialog.dismiss();            }        });        return alertDialogBuilder.create();    }    public static MyDialogFragment newInstance() {        MyDialogFragment = new MyDialogFragment;        return f;    }}
解决方法 您没有名为newInstance的静态方法.在对话框片段中添加以下内容
public static MyDialogFragment newInstance() {    MyDialogFragment f = new MyDialogFragment();    return f;    }

您可以在文档中找到更多信息和示例

http://developer.android.com/reference/android/app/DialogFragment.html

总结

以上是内存溢出为你收集整理的android – 从Fragment调用DialogFragment全部内容,希望文章能够帮你解决android – 从Fragment调用DialogFragment所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1130377.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存