在我的主视图应用程序中有3个选项卡。这3个标签填充了带有查看页面适配器的独立片段。在我的第二个选项卡中,有一个列表视图(包含在一个片段中)。当用户点击一个列表视图时,我需要在那里显示另一个片段。但是,当用户点击一个列表视图项目时,我得到了这样的错误片段内的Listview onitemclick不起作用
No view found for id 0x7f0e008a (com.example.abc.myapp:id/new_output) for fragment MantraFragment{30391e5 #3 id=0x7f0e008a}
我在其他问题在stackoverflow中发现此错误。试过他们的解决方案,但没有奏效。请帮我解决这个问题。
类,其中包括列表视图
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开发初学者,我认为这是很好用的一个片段有insted的展示活动中。 (纠正我,如果我错了)
你可以请张贴有关您是如何加载标签页的代码?可能是你正在加载基片段容器中的子片段。这就是你遇到问题的地方。 –
@ TrickySolutons-非常感谢。我已经更新了代码。 – venura
'<片段 机器人:layout_width = “match_parent” 机器人:layout_height = “match_parent” 机器人:背景= “#b0b0ff” 机器人:ID = “@ + ID/new_output”/>' 上面的FrameLayout替换 '<的FrameLayout 机器人:layout_width = “match_parent” 机器人:layout_height = “match_parent” 机器人:背景= “#b0b0ff” 机器人:ID = “@ + ID/new_output”/>' 应该解决您问题 –