I know there are several similar like this already, but none of them have helped me out, and I have also tried searching the web but no solution.
Anyway, I have a listfragment (fragA) that is launched from a FragmentActivity. FragA contains a button that when clicked, it should open another ListFragment(fragB).
Everything looks fine except that i get this exception in fragB:
unsupportedoperationexception addview is not supported in adapterview
Please how can I resolve this?
Thanks.
Code:
FRAG A:
public class FragA extends ListFragment {
public static ListView _listView;
private CAdapter _adapter;
private ObjCollection _data;
private Context _context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.layout, null);
_listView = (ListView) view.findViewById(android.R.id.list);
_data = new ObjCollection();
Button btn = (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavigateFragment(new FragB(), true, FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
_adapter = new CAdapter(getActivity(), _data);
_listView.setAdapter(_contactAdapter);
}
@Override
public void onStart() {
super.onStart();
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
private void NavigateFragment(ListFragment listFragment, Boolean addtostack, int transition) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.list, listFragment);
ft.setTransition(transition);
if (addtostack)
ft.addToBackStack(null);
ft.commit();
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/button" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@id/android:empty"
style="@style/DefaultText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:gravity="center_horizontal"
android:text="empty"
android:textSize="25sp" />
</FrameLayout>
<Button
android:id="@+id/button"
style="@style/buttonText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="button" />
FRAG B:
public class FragB extends ListFragment {
public static ListView _listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.layoutB, null);
_listView = (ListView) view.findViewById(android.R.id.list);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@id/android:empty"
style="@style/DefaultText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:gravity="center_horizontal"
android:textSize="25sp" />
</FrameLayout>
</RelativeLayout>
LOGCAT:
12-28 11:40:53.368: E/AndroidRuntime(15426): FATAL EXCEPTION: main
12-28 11:40:53.368: E/AndroidRuntime(15426): java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
12-28 11:40:53.368: E/AndroidRuntime(15426): at android.widget.AdapterView.addView(AdapterView.java:454)
12-28 11:40:53.368: E/AndroidRuntime(15426): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:922)
12-28 11:40:53.368: E/AndroidRuntime(15426): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-28 11:40:53.368: E/AndroidRuntime(15426): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-28 11:40:53.368: E/AndroidRuntime(15426): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-28 11:40:53.368: E/AndroidRuntime(15426): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
12-28 11:40:53.368: E/AndroidRuntime(15426): at android.os.Handler.handleCallback(Handler.java:730)
12-28 11:40:53.368: E/AndroidRuntime(15426): at android.os.Handler.dispatchMessage(Handler.java:92)
12-28 11:40:53.368: E/AndroidRuntime(15426): at android.os.Looper.loop(Looper.java:137)
12-28 11:40:53.368: E/AndroidRuntime(15426): at android.app.ActivityThread.main(ActivityThread.java:5450)
12-28 11:40:53.368: E/AndroidRuntime(15426): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 11:40:53.368: E/AndroidRuntime(15426): at java.lang.reflect.Method.invoke(Method.java:525)
12-28 11:40:53.368: E/AndroidRuntime(15426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
12-28 11:40:53.368: E/AndroidRuntime(15426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
12-28 11:40:53.368: E/AndroidRuntime(15426): at dalvik.system.NativeStart.main(Native Method)
Please how can I resolve this?
You're using an incorrect id in the transaction to show the second fragment. Right now in the NavigateFragment()
method you have:
//...
ft.replace(android.R.id.list, listFragment);
ft.setTransition(transition);
//...
so you're using the id of the ListView
from the first ListFragment
as the transaction's container, so the ListView
will be the container for the second ListFragment
. This is wrong and it will fail when the FragmentManager
will try to add the view of the second fragment to the ListView
(as ListView
doesn't allow the direct use of the addView()
method).
The solution is to use in the NavigateFragment()
method the id of the container where the first ListFragment
is added instead of android.R.id.list
.