Listview里面的片段onitemclick无效

时间:2017-04-14 11:18:21

标签: android android-fragments

在我的主视图应用程序中有3个选项卡。这3个标签填充了带有viewpage适配器的单独片段。在我的第二个标签中,有一个列表视图(包含在一个片段中)。当用户点击列表视图时,我需要在那里显示另一个片段。但是,当用户点击listview项时,我收到了类似这样的错误

No view found for id 0x7f0e008a (com.example.abc.myapp:id/new_output) for fragment MantraFragment{30391e5 #3 id=0x7f0e008a}

我在stackoverflow中的其他问题中发现了这个错误。尝试过他们的解决方案,但没有奏效。请帮我解决这个问题。

包含listview

的类
public class TwoFragment extends Fragment  {
private ListView listView;
private String items[];


public TwoFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    items = getResources().getStringArray(R.array.races_array);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_two, container, false);

    listView = (ListView) rootView.findViewById(R.id.mylistView);
    CustomListAdapter adapter = new CustomListAdapter(getActivity(), items);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            FragmentManager fm = getActivity().getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();


            Fragment newFrag = new MyNewFragment();
            Bundle arguments = new Bundle();
            newFrag.setArguments(arguments);
            ft.replace(R.id.new_output, newFrag);
            ft.addToBackStack(null);
            ft.commit();

        }
    });

    return rootView;
}}

MyNewFragment.class

public class MyNewFragment extends Fragment{

public MyNewFragment(){
   }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView= inflater.inflate(R.layout.mynewfrag_view, container, false);

    return rootView;
}}

MyNewFragment视图(mynewfrag_view)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"

    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#FF0000"
        android:textSize="20sp"
        android:id="@+id/msg2"/>


    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#b0b0ff"
        android:id="@+id/new_output" />


</LinearLayout>

MainActivity类

public class MainActivity extends AppCompatActivity  {

   private Toolbar toolbar;
   private TabLayout tabLayout;
   private ViewPager viewPager;



  @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);

       viewPager = (ViewPager) findViewById(R.id.viewpager);
       setupViewPager(viewPager);

       tabLayout = (TabLayout) findViewById(R.id.tabs);
       tabLayout.setupWithViewPager(viewPager);
   }


 private void setupViewPager(ViewPager viewPager) {
       ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
       adapter.addFragment(new OneFragment(), "One");
       adapter.addFragment(new TwoFragment(), "Two");
       adapter.addFragment(new ThreeFragment(), "Three");

   viewPager.setAdapter(adapter);
   }



 public class ViewPagerAdapter extends FragmentPagerAdapter {
       private final List<Fragment> mFragmentList = new ArrayList<>();
       private final List<String> mFragmentTitleList = new ArrayList<>();

   public ViewPagerAdapter(FragmentManager manager) {
       super(manager);
   }

   @Override
   public Fragment getItem(int position) {
       return mFragmentList.get(position);
   }

   @Override
   public int getCount() {
       return mFragmentList.size();
   }

   public void addFragment(Fragment fragment, String title) {
       mFragmentList.add(fragment);
       mFragmentTitleList.add(title);
   }

   @Override
   public CharSequence getPageTitle(int position) {
       return mFragmentTitleList.get(position);
   }   }}

MainActivity布局(activity_main)

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

PS:我还是Android开发的初学者,我认为使用一个显示活动的片段是件好事。 (如果我错了,请纠正我)

2 个答案:

答案 0 :(得分:0)

您正在添加MyNewFragment并提供id中存在的my_newfragment.xml视图,该视图仍然未知。您必须在TwoFragment的布局文件中添加/替换片段,即fragment_two.xml

您的fragment_two.xml应该是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">

    ....

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#b0b0ff"
        android:id="@+id/new_output" />

    ....

</LinearLayout>

fragment移除new_output代码mynewfrag_view.xml,然后像上面一样添加fragment_two.xml,并检查它是否有效。

答案 1 :(得分:0)

这不能识别在TwoFragment类的onclicklistener中使用的R.id.new_output ft.replace(R.id.new_output, newFrag);