You need to use a Theme.AppCompat theme (or descendant) with this activity
今天做一个dialog的时候发现show的时候一直报use a Theme.AppCompat theme这个错误,字面意思上就是要使用AppCompat类型的主题,因为有用到toolbar,一直用的这个theme:AppTheme.NoActionBar
按照提示改成Theme.AppCompat.Light.NoActionBar,依旧报错,设置改成Theme.AppCompat,依旧出错,瞬间无语了 查看了theme的源码发现没有,对应activity的主题的没错,谷歌了一轮没有发现自己想要的答案
代码块
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("警告");
builder.setMessage(getString(R.string.warnning_register));
builder.setPositiveButton("好的", null);
builder.setCancelable(false);
builder.show();
“`
再看一眼代码,AlertDialog.Builder(getContext());终于发现了问题所在,最近刚联系MVP,实现getContext顺手写了getApplicationContext(),获取到的是application而不是当前的activity,自然主题怎么变都没效果了…囧
这个错误以前犯过,没想到还是不长记性,Context还是有所不同的,还是老老实实的写activity.this吧!
希望这回好好长记性了!