Android 怎么在Fragment里弹出对话框????
Android 的碎片(Fragment)里调用静态类的Dialog出错
错误说明
先上代码
这是自定义的一个对话框静态类,为了方便我把它们封装起来了,但是在碎片里直接调用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里调用弹出对话框啊?