ase*_*lin 27 android android-appcompat
我在我的应用程序中使用AppCompat库(com.android.support:appcompat-v7:22.1.0).我在一个片段中创建了一个ActionBar.当我单击菜单项时,它会显示一个警告对话框.这是我的代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_new:
showFilterDialog();
return true;
case R.id.action_send:
new sendInventoryTask().execute();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
和我的showInventoryDialog方法:
private void showFilterInventoryDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
LayoutInflater inflater= getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_filter_inventory,null);
alert.setView(v);
alert.setTitle(getResources().getString(R.string.filters));
alert.setPositiveButton(getResources().getString(R.string.filter), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// TODO
}
});
alert.setNegativeButton(getResources().getString(R.string.cancel), null);
alert.show();
}
Run Code Online (Sandbox Code Playgroud)
一切正常,但当我点击菜单项时,logcat显示错误:
I/AppCompatDelegate:Activity的LayoutInflater已安装了Factory,因此我们无法安装AppCompat的
怎么解决这个?
据我所知,appcompat 23.1.1 installViewFactory()在AppCompatDelegateImplV7每次显示创建的对话框时都会调用AlertDialog.Builder.
调用堆栈:
位于android.support.v7.app.AppCompatDialog.onCreate(AppCompatDialog.java:58)的android.support.v7.app.AppCompatDelegateImplV7.installViewFactory(AppCompatDelegateImplV7.java:970),位于android.support.v7.app.AlertDialog.onCreate (AlertDialog.java:239)在android.app.Dialog.dispatchOnCreate(Dialog.java:361)的android.app.Dialog.show(Dialog.java:262)android.support.v7.app.AlertDialog $ Builder.秀(AlertDialog.java:902)
@Override
public void installViewFactory() {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
if (layoutInflater.getFactory() == null) {
LayoutInflaterCompat.setFactory(layoutInflater, this);
} else {
Log.i(TAG, "The Activity's LayoutInflater already has a Factory installed"
+ " so we can not install AppCompat's");
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,一旦工厂已经设置,它只记录信息性消息.忽略它似乎很安全,但是当它填满你的日志时会很烦人.
小智 6
在这种情况下,您需要使用主题上下文,即代替
new AlertDialog.Builder(getActivity());
你必须做
new AlertDialog.Builder(getSupportActionBar().getThemedContext());
Run Code Online (Sandbox Code Playgroud)
此外,你还需要遵循这里给出的父主题和windowActionBar提示 - support.v7.app.AlertDialog抛出NullPointerException关闭
| 归档时间: |
|
| 查看次数: |
11750 次 |
| 最近记录: |