在两个列表片段之间导航

Capiono

我知道已经有几种类似的方法,但是它们都没有帮助我,我也尝试过搜索网络,但是没有解决方案。

无论如何,我有一个从FragmentActivity启动的listfragment(fragA)。FragA包含一个按钮,单击该按钮时应打开另一个ListFragment(fragB)。

一切看起来都很好,除了我在fragB中得到了这个例外:

adapterview不支持unsupportedoperationexception addview

请问我该如何解决?

谢谢。

代码:

片段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" />

问题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)
用户

请问我该如何解决?

您在事务中使用错误的ID来显示第二个片段。现在,在该NavigateFragment()方法中,您具有:

//...
ft.replace(android.R.id.list, listFragment);
ft.setTransition(transition);
//...

让你在使用的标识ListView从第一个ListFragment作为交易的容器,所以ListView将成为第二个容器ListFragment这是错误的,并且会在FragmentManager尝试将第二个片段的视图添加到中时失败ListView(因为ListView不允许直接使用该addView()方法)。

解决方法是在NavigateFragment()方法中使用添加第一个容器的容器ListFragment的id而不是android.R.id.list

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章