单击ListView中的图像按钮以导航到ViewPager片段

时间:2014-09-24 13:37:12

标签: android android-fragments android-listview

我有一个使用Custom BaseAdapter的ListView片段。在我的ListView中,我想单击图像并调用包含ViewPager的Ne​​w Fragment。不知怎的,我收到此错误消息java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView并且无法弄清问题是什么。我试过搜索但没有解释我的错误信息。 我做错了什么?

    java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
        at android.widget.AdapterView.addView(AdapterView.java:452)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

我的onClickListener

                imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    MOKPagerFragment fragment2 = new MOKPagerFragment();
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.listview_fragment,fragment2);
                    fragmentTransaction.commit();

                }
            });

这是我的布局

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

主要布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/fragment_container"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

1 个答案:

答案 0 :(得分:0)

imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    MOKPagerFragment fragment2 = new MOKPagerFragment();
                    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.fragment_container,fragment2);
                    fragmentTransaction.commit();

                }
            });