【问题标题】:java.lang.illegalstateexception: a factory has already been set on this layoutinflaterjava.lang.illegalstateexception: 已经在这个 layoutinflater 上设置了一个工厂
【发布时间】:2012-11-16 11:02:57
【问题描述】:

我尝试在我的 android 应用中更改选项菜单的背景颜色。我正在使用 ActionBarSherlock 库。我已经尝试使用此代码更改选项菜单的背景颜色

https://stackoverflow.com/a/8475357/584095

但我最终在行中遇到了异常“java.lang.illegalstateexception:已在此 layoutinflater 上设置了工厂”

LayoutInflater.setFactory();

我不知道这段代码有什么问题。谁能帮我解决这个问题?

【问题讨论】:

    标签: android background-color options-menu android-optionsmenu oncreateoptionsmenu


    【解决方案1】:

    自版本 22.1.0 起,支持库中有一个 change

    如果您尝试调用 getLayoutInflater().setFactory(),您将收到 IllegalStateException

    你应该使用新的 api

    或者干脆使用旧版本

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        要保持兼容性库正常工作并避免“java.lang.illegalstateexception:已在此 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) {
            // ...
        }
        

        【讨论】:

        • 不起作用。抛出“android.view.InflateException: Binary XML file line #17: Error inflating class com.android.internal.view.menu.ActionMenuItemView”
        • 实际上不再抛出异常,但我的文本颜色仍然是灰色:(
        【解决方案4】:

        这对我有用:

        LayoutInflater inflater = LayoutInflater.from(context);
        if (inflater.getFactory() != null) {
            inflater = inflater.cloneInContext(context);
        }
        inflater.setFactory(factory);
        

        【讨论】:

          【解决方案5】:

          我遇到了同样的问题,但没有答案帮助我。

          就我而言:

          • 我正在扩展 AppCompatActivity

          • 项目定位 API 27

          • 实现支持 lib 27.1.1

          解决方案: 显然,现在不需要将 Factory 设置为 LayoutInflater,它会崩溃或被忽略。只需覆盖 AppCompatActivity 中的两个方法 onCreateView(...) (来自 android.support.v4.app.BaseFragmentActivityApi14)

          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);
              }
              ...
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-11-03
            • 2017-10-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-07-24
            相关资源
            最近更新 更多