0

This is not the same situation with Logcat error: "addView(View, LayoutParams) is not supported in AdapterView" in a ListView

I start a Fragment in ActivityA:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frag_a, new FragmentA());
ft.commit();

There is a ListView in FragmentA's layout, when the ListView's item is clicked, it will replace R.id.frag_a with another fragment, code like these:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frag_a, new FragmentB);
ft.commit();

And FragmentA's onCreateView method:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View tview = inflater.inflate(R.layout.frag_list_a, container, false);

    mListView = (ListView) tView.findViewById(R.id.listview1);

    return tview;
}

FragmentB's onCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    if (container != null) {
            System.out.println("+++ container: " + container.getClass().getName());
    }
    View view = inflater.inflate(R.layout.frag_content_a, null, false);

    System.out.println("+++ view: " + view.getClass().getName());

    return view;
}    

the problem is, when I click the item in FragmentA's ListView, Exception throwed:

ERROR/AndroidRuntime(321): java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
    at android.widget.AdapterView.addView(AdapterView.java:435)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:848)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1359)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:411)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)

I checked the implement of FragmentManagerImpl, when the Fragment is create by code (not from layout), it will add the fragment's view into mContainer:

if (!f.mFromLayout) {
    ViewGroup container = null;
    if (f.mContainerId != 0) {
        container = (ViewGroup)mActivity.findViewById(f.mContainerId);
        if (container == null && !f.mRestored) {
            throw new IllegalArgumentException("No view found for id 0x"
                    + Integer.toHexString(f.mContainerId)
                    + " for fragment " + f);
        }
    }
    f.mContainer = container;
    f.mView = f.onCreateView(f.getLayoutInflater(f.mSavedFragmentState),
            container, f.mSavedFragmentState);
    if (f.mView != null) {
        f.mView.setSaveFromParentEnabled(false);
        if (container != null) {
            Animator anim = loadAnimator(f, transit, true,
                    transitionStyle);   
            if (anim != null) { 
                anim.setTarget(f.mView);
                anim.start();       
            }
            container.addView(f.mView);
        }
        if (f.mHidden) f.mView.setVisibility(View.GONE);
        f.onViewCreated(f.mView, f.mSavedFragmentState);
    }
}

I print out the container in FragmentB's onCreateView method, It's FragmentA's ListView. Since ListView extends AdapterView, and AdapterView's addView throw UnsupportedOperationException directly, this is why the exception occur.

But I just do not know how to fix it. Appreciate your help.

Community
  • 1
  • 1
Eric Yu
  • 11
  • 1
  • 4

2 Answers2

1

When you inflate a layout in ArrayAdapter's getView, set the parent parameter to null, as in View view = inflater.inflate(R.layout.mylayout, null);

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
  • 1
    This does fix the exception, and in my experience this also happens with BaseAdapter. But why? Why shouldn't we use the parent view passed to us? Why does it get passed? Sorry for the questions, I'm just curious. – IT-Dan Jul 06 '14 at 12:40
0

FragmentA should extends ListFragment, and don't overwrite the onCreateView method.
see the follow code:

package org.goodev.fragment;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class FragmentA extends ListFragment {

    private OnItemClickListener mOnItemClickListener;

    private static final String[] GENRES = new String[] {
        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
    };

    private ArrayAdapter<String> mListAdapter;

    public FragmentA() {
        super();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mListAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, GENRES);
    }

    @Override
    public void onStart() {
        super.onStart();
        setListAdapter(mListAdapter);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        getListView().setOnItemClickListener(listener);
    }

}
Goodev
  • 1
  • 1
  • Thanks Goodev, but I checked the ListFragment [source](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/app/ListFragment.java) implement, there is nothing special in it, can you tell me why FragmentB's container is FragmentA's ListView. – Eric Yu Mar 07 '12 at 07:15
  • I don't know what's the R.id.frag_a on the Layout xml file. I write a demo, you can find from there: http://goodev-demo-code.googlecode.com/files/FragmentTest.zip – Goodev Mar 07 '12 at 10:39