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.