Note that there are some explanatory texts on larger screens.

plurals
  1. POI got "addView(View) is not supported in AdapterView" error when use Fragment
    text
    copied!<p>This is not the same situation with <a href="https://stackoverflow.com/questions/4576219">Logcat error: &quot;addView(View, LayoutParams) is not supported in AdapterView&quot; in a ListView</a></p> <p>I start a Fragment in ActivityA:</p> <pre><code>FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.frag_a, new FragmentA()); ft.commit(); </code></pre> <p>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:</p> <pre><code>FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.frag_a, new FragmentB); ft.commit(); </code></pre> <p>And FragmentA's onCreateView method:</p> <pre><code>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; } </code></pre> <p>FragmentB's onCreateView:</p> <pre><code>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; } </code></pre> <p>the problem is, when I click the item in FragmentA's ListView, Exception throwed:</p> <pre><code>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) </code></pre> <p>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:</p> <pre><code>if (!f.mFromLayout) { ViewGroup container = null; if (f.mContainerId != 0) { container = (ViewGroup)mActivity.findViewById(f.mContainerId); if (container == null &amp;&amp; !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); } } </code></pre> <p>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.</p> <p>But I just do not know how to fix it. Appreciate your help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload