Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

android - The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's

I'm using AppCompat library (com.android.support:appcompat-v7:22.1.0) in my app. I created an ActionBar in a fragment. When I click in a menu item it shows an Alert Dialog. Here is my code:

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

And my showInventoryDialog method:

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

Everything works fine, but when I click on menu item, the logcat shows me an error:

I/AppCompatDelegate﹕ The Activity's LayoutInflater already has a Factory installed so we can not install AppCompat's

How to solve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From what I can tell, appcompat 23.1.1 calls installViewFactory() in AppCompatDelegateImplV7 every time you show a dialog created by AlertDialog.Builder.

Call Stack:

at android.support.v7.app.AppCompatDelegateImplV7.installViewFactory(AppCompatDelegateImplV7.java:970) at android.support.v7.app.AppCompatDialog.onCreate(AppCompatDialog.java:58) at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:239) at android.app.Dialog.dispatchOnCreate(Dialog.java:361) at android.app.Dialog.show(Dialog.java:262) at android.support.v7.app.AlertDialog$Builder.show(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");
        }
    }

As you can see, once the factory is already set it just logs the informational message. It seems pretty safe to ignore, but it can get annoying when it fills up your logs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...