gpt4 book ai didi

java - 如何在 PageViewer 中显示 3 个 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:15 25 4
gpt4 key购买 nike

在一个 fragment 中,我试图创建一个包含 3 个 fragment 的 ViewPager,您可以在它们之间滑动。我无法理解结构和我得到的错误..

public class ContactFragment extends Fragment {

FragmentPagerAdapter adapterViewPager;
private FragmentActivity myContext;

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

ViewPager vpPager = (ViewPager) rootView.findViewById(R.id.vpPager);
adapterViewPager = new MyPagerAdapter(myContext.getSupportFragmentManager());
vpPager.setAdapter(adapterViewPager);

return rootView;
}

@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}

public static class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 3;

public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}

// Returns total number of pages
@Override
public int getCount() {
return NUM_ITEMS;
}

// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return FirstFragment.newInstance(0, "Page # 1");
case 1: // Fragment # 0 - This will show FirstFragment different title
return FirstFragment.newInstance(1, "Page # 2");
default:
return null;
}
}

// Returns the page title for the top indicator
@Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}

}
}

我的 ViewPager 布局 xml 如下所示:

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

<android.support.v4.view.ViewPager
android:id="@+id/vpPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="4dp"
android:paddingTop="4dp" />

</android.support.v4.view.ViewPager>
</LinearLayout>

这是我的文本 fragment 类,我从中实例化了 3 个 fragment :

public class FirstFragment extends Fragment {
// Store instance variables
private String title;
private int page;

// newInstance constructor for creating fragment with arguments
public static FirstFragment newInstance(int page, String title) {
FirstFragment fragmentFirst = new FirstFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}

// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
}

// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel);
tvLabel.setText(page + " -- " + title);
return view;
}
}

当我运行应用程序并加载 fragment 时它看起来是正确的,但是当我尝试在页面查看器中滑动到新 fragment 时它崩溃了:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: org.example.magnusluca.drawertestapp, PID: 3624
java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference

最佳答案

使用这个

@Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return FirstFragment.newInstance(0, "Page # 1");
case 1: // Fragment # 0 - This will show FirstFragment different title
return FirstFragment.newInstance(1, "Page # 2");
case 2:
return FirstFragment.newInstance(2, "Page # 3");
default:
return new Fragment();
}
}

你应该有 3 个 switch case 并且你的 default case 不应该返回 null

关于java - 如何在 PageViewer 中显示 3 个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26822516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com