0

这与Logcat 错误的情况不同:ListView 中的“addView(View, LayoutParams) is not supported in AdapterView”

我在 ActivityA 中启动了一个片段:

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

FragmentA的布局中有一个ListView,当ListView的item被点击时,会用另一个fragment替换R.id.frag_a,代码如下:

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

和 FragmentA 的 onCreateView 方法:

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 的 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;
}    

问题是,当我单击 FragmentA 的 ListView 中的项目时,抛出异常:

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)

我检查了 FragmentManagerImpl 的实现,当 Fragment 是通过代码(不是从布局)创建时,它会将 Fragment 的视图添加到 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);
    }
}

我在 FragmentB 的 onCreateView 方法中打印出容器,它是 FragmentA 的 ListView。由于ListView扩展了AdapterView,而AdapterView的addView直接抛出UnsupportedOperationException,这就是异常发生的原因。

但我只是不知道如何解决它。感谢你的帮助。

4

2 回答 2

1

当您在ArrayAdapter's中对布局进行膨胀时getView,将parent参数设置为null,如View view = inflater.inflate(R.layout.mylayout, null);

于 2013-06-20T00:01:46.990 回答
0

FragmentA 应该继承 ListFragment,并且不要覆盖 onCreateView 方法。
见以下代码:

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

}
于 2012-03-07T02:10:08.673 回答