使用OnItemClickListener列出视图片段到片段事务

时间:2019-03-24 12:30:42

标签: android android-fragments android-listview

我有两个片段。

public class ListNav extends Fragment
public class SwipeNav extends Fragment

我使用“ ListNav”类制作了列表视图。单击字符串数组的任何项目时,我想使用“ setOnItemClickListener”将片段处理为SwipeNav片段。 使用以下内容时我得到结果。

 @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    lv = (ListView) view.findViewById(R.id.my_listView);
    inputS=(TextView) view.findViewById(R.id.inputSearch);

    ArrayAdapter<CharSequence> aa = ArrayAdapter.createFromResource(getContext(), R.array.tab_titles, android.R.layout.simple_list_item_1);
    lv.setAdapter(aa);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
         inputS.setText((String) lv.getAdapter().getItem(i));

        }
    });

但是我需要单击'ListNav'上的字符串数组中的项,它将打开新的片段'SwipeNav',该片段会打开带有滑动视图的同一字符串数组中的所有项。我想将相同的项目从ListNav显示到SwipeNav上的相同项目。 为此,我无法实现。我使用以下方法,但失败。错误信息 : 不幸的是,应用程序停止了。

            SwipeNav fragment = new SwipeNav();
            FragmentTransaction transaction = 
            getFragmentManager().beginTransaction();
            transaction.replace(R.id.nav_list, fragment);
            transaction.addToBackStack(null);
            transaction.commit(); 

SwipeNav.java

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    myPagerAdapter = new 
    MyPagerAdapter(getChildFragmentManager(),getContext());
    viewPager = (ViewPager) view.findViewById(R.id.pager);
    viewPager.setAdapter(myPagerAdapter);

Logcat错误:

                Shutting down VM
                W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4d09b20)
                E/AndroidRuntime: FATAL EXCEPTION: main
                java.lang.IllegalArgumentException: No view found for id 0x7f090067  (..:id/nav_list) for fragment SwipeNav{529dd108 #2 id=0x7f090067}
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1413)
                  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
                  at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
                  at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
                  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
                  at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
                  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:5001)
                  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:785)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                  at dalvik.system.NativeStart.main(Native Method)

SwipeNav.java也在“导航”菜单内。它将打开所有带有滑动视图的项目。 SwipeNav没有错误。

1 个答案:

答案 0 :(得分:0)

R.id.nav_list相对应的视图是否在活动中?如果是这样,您的片段找不到活动中的视图。要么向活动抛出回调,然后替换活动中的片段,要么使用view.rootView.findViewById(R.id.nav_list)获取视图并给出其ID。