从ViewPagerAdapter访问时,Android片段始终为NULL

时间:2017-06-14 00:11:26

标签: java android android-fragments android-viewpager

问题: 在我的主要活动的onCreate()中,我试图在我的片段中设置TextView的文本值,该值由viewpager显示。当我尝试获取我的片段实例时,它总是返回null。

我试过的解决方案: 几乎所有与SO上的这个问题相关的解决方案都给出了相同的结果(NPE)。

我的代码如下以及一些解释......

Home.java活动(我的主要活动):

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

        // Show the home layout
        setContentView(R.layout.home);

        // Setup the toolbar
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Now setup our tab bar
        TabLayout tabLayout = (TabLayout)findViewById(R.id.homeTabs);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        // Setup the view pager
        ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

        // Start our service controller
        Intent startServiceController = new Intent(this, ServiceController.class);
        startService(startServiceController);

        Fragment viewFragment = pagerAdapter.getRegisteredFragment(viewPager.getCurrentItem());

        if(viewFragment == null){
            Log.v("Fragment", "NULL");
        }
    }

设置pageAdapter和viewpager后,我尝试访问正在显示的片段。我的PagerAdapter的代码低于......

PagerAdapter:

public class PagerAdapter extends SmartFragmentStatePagerAdapter {

    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new ParkListFragment();
            case 1:
                return new MapFragment();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        CharSequence title;

        switch (position){
            case 0:
                title = "Spaces Near You";
                break;
            case 1:
                title = "Map";
                break;
            default:
                title = "N/A";
                break;
        }

        return title;
    }
}

SmartFragmentStatePagerAdapter:

public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
    // Sparse array to keep track of registered fragments in memory
    private SparseArray<Fragment> registeredFragments = new SparseArray<>();

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

    // Register the fragment when the item is instantiated
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    // Unregister when the item is inactive
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    // Returns the fragment for the position (if instantiated)
    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }
}

我真的不明白这是什么问题。我尝试了很多东西。我不想用的是:

String tag="android:switcher:" + viewId + ":" + index;

ParkListFragment.java:

public class ParkListFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.park_list_fragment, container, false);
    }
}

ParkList片段布局:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerInParent="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/confidenceText"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textColor="@android:color/primary_text_light"
                android:padding="10dp"/>

        </LinearLayout>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerInParent="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/activityText"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textColor="@android:color/primary_text_light"
                android:padding="10dp"/>

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

堆栈追踪:Pastebin

1 个答案:

答案 0 :(得分:0)

经过几个小时的反复试验,对我来说唯一有用的就是这个SO帖子:

ViewPager Fragments are not initiated in onCreate