Android 怎么在Fragment里弹出对话框????

错误说明

先上代码

这是自定义的一个对话框静态类,为了方便我把它们封装起来了,但是在碎片里直接调用MyDialog.showMessage();出错了。
Context 可能是这个问题错了,我觉得肯定是他出错了,我是这样实例化context的,也不知道对不对,目前来讲,我在操作数据库的时候,这样子用context没问题。但是要用来显示对话框就不对了。
在这里插入图片描述
这是静态类的对话框调用

//显示简单消息框
    public static void showMessage(Context context,String title,String message)
    {
        View myview=LayoutInflater.from(context).inflate(R.layout.layout_dialog_showmessage,null);
        TextView textView_dialog_message1=(TextView)myview.findViewById(R.id.textView_dialog_message1);
        TextView textView_dialog_message2=(TextView)myview.findViewById(R.id.textView_dialog_message2);
        Button button_dialog_message_sure=(Button)myview.findViewById(R.id.button_dialog_message_sure);
        textView_dialog_message1.setText(title);
        textView_dialog_message2.setText(message);
        button_dialog_message_sure.setWidth(MyPhone.getScreenWidth(context)/2);
        AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.my_dialog_message);//对话框主题
        final AlertDialog mAlertDialog = builder.create();
        mAlertDialog.setCanceledOnTouchOutside(false);
        mAlertDialog.setCancelable(true);
        button_dialog_message_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAlertDialog.dismiss();
            }
        });
        mAlertDialog.show();
        mAlertDialog.getWindow().setContentView(myview);
        mAlertDialog.getWindow().setLayout(MyPhone.getScreenWidth(context)-200,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        mAlertDialog.getWindow().setWindowAnimations(R.style.my_dialog_bottom_style);//动画
    }

然后在Fragment里调用。。。

MyDialog.showMessage(MyApplycation.getContext().getApplicationContext(),“显示对话框”,“123”);

再然后报错


     java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:555)
        at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:466)
        at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:94)
        at android.support.v7.app.AlertController.installContent(AlertController.java:232)
        at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:279)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:389)
        at android.app.Dialog.show(Dialog.java:293)
        at com.lkl.dialog.MyDialog.showMessage(MyDialog.java:201)
        at com.lkl.activity.ui.Min_My_Page$3.onClick(Min_My_Page.java:196)
        at android.view.View.performClick(View.java:5619)
        at android.view.View$PerformClick.run(View.java:22298)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
        在这里插入代码片

如果我简单一点,在fragment弹出对话框,也是报错
在这里插入图片描述

已尝试解决办法

1.网上说可以将对话框直接继承activity

但是不知道怎么用啊
在这里插入图片描述

2.有些博客说到,对话框依赖activity,那么我在MainActivity的时候,把该activity放到静态变量中,直接调用,但是也失败了。

最后,求助啊

怎么在Fragment里调用弹出对话框啊?

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值