java.lang.IllegalStateException: 在这个LayoutInflater上已经设置了一个工厂

9

我尝试着改变我的Android应用的选项菜单背景颜色。我正在使用ActionBarSherlock库。我尝试了这段代码来改变选项菜单的背景颜色

https://dev59.com/uHA85IYBdhLWcg3wF_i0#8475357

但我遇到了一个异常"java.lang.illegalstateexception: a factory has already been set on this layoutinflater",出现在这一行:

LayoutInflater.setFactory();

我不知道这段代码哪里出了问题。有人能帮我解决这个问题吗?
5个回答

6

自22.1.0版本以来,支持库已经 更改

如果您尝试调用getLayoutInflater().setFactory(),将会出现IllegalStateException异常。

您应该使用新的api

或者简单地使用旧版本。

  • com.android.support:appcompat-v7:22.0.0
  • com.android.support:appcompat-v4:22.0.0

5
为了使兼容性库正常工作并避免出现“java.lang.illegalstateexception: a factory has already been set on this layoutinflater”错误,您需要获取已设置的工厂的最终引用,并在自己的Factory.onCreateView中调用其onCreateView方法。在此之前,必须使用内省技巧允许您再次为LayoutInflater设置一个工厂:
LayoutInflater layoutInflater = getLayoutInflater();
final Factory existingFactory = layoutInflater.getFactory();
// use introspection to allow a new Factory to be set
try {
    Field field = LayoutInflater.class.getDeclaredField("mFactorySet");
    field.setAccessible(true);
    field.setBoolean(layoutInflater, false);
    getLayoutInflater().setFactory(new Factory() {
        @Override
        public View onCreateView(String name, final Context context, AttributeSet attrs) {
            View view = null;
            // if a factory was already set, we use the returned view
            if (existingFactory != null) {
                view = existingFactory.onCreateView(name, context, attrs);
            }
            // do whatever you want with the null or non-null view
            // such as expanding 'IconMenuItemView' and changing its style
            // or anything else...
            // and return the view
            return view;
        }
    });
} catch (NoSuchFieldException e) {
    // ...
} catch (IllegalArgumentException e) {
    // ...
} catch (IllegalAccessException e) {
    // ...
}

1
不起作用。抛出“android.view.InflateException:Binary XML file line #17: Error inflating class com.android.internal.view.menu.ActionMenuItemView”错误。 - MSIslam
实际上不再抛出异常,但我的文本颜色仍然是灰色 :( - To Kra

5

这是由于您使用了兼容性库。它设置了自己的工厂来处理特定平台的布局。 您可以尝试在调用super.onCreate()之前在onCreate()方法中设置自己的工厂。这将禁止兼容性库覆盖工厂,并使您无法从xml文件中填充片段,但样式应该有效。


3
这对我有效:
LayoutInflater inflater = LayoutInflater.from(context);
if (inflater.getFactory() != null) {
    inflater = inflater.cloneInContext(context);
}
inflater.setFactory(factory);

0

我也遇到了同样的问题,但是没有找到有用的答案。

我的情况如下:

  • 我正在扩展AppCompatActivity

  • 项目目标API 27

  • 实现支持库27.1.1

解决方案: 显然,现在无需将工厂设置为LayoutInflater,否则它会崩溃或被忽略。只需要在您的AppCompatActivity(从android.support.v4.app.BaseFragmentActivityApi14)中覆盖onCreateView(...)方法即可。

public class myActivity extends AppCompatActivity  {
    ...
    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        if (name.compareTo("EditText") == 0) {
            CustomEdit newEdit = new CustomEdit(this,attrs); 
            return newEdit;
        }
        return super.onCreateView(parent, name, context, attrs);
    }
    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return onCreateView(null, name, context, attrs);
    }
    ...
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接