【问题标题】:Android Fragment Always NULL When Accessing From ViewPagerAdapter从 ViewPagerAdapter 访问时,Android Fragment 始终为 NULL
【发布时间】:2017-06-16 03:40:39
【问题描述】:

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

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

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

Home.java Activity(我的主Activity):

    @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

【问题讨论】:

  • 请添加您的堆栈跟踪
  • @ArunShankar 我用 pastebin 的链接进行了编辑。
  • 你的 logact 异常但整个异常在哪里?
  • @MoussaHarajli 你有类扩展BaseAdapterArrayAdapter 吗?你真的在你的应用中使用Listview吗?

标签: java android android-fragments android-viewpager


【解决方案1】:

所以经过数小时的反复试验,唯一对我有用的是这个 SO 帖子:

ViewPager Fragments are not initiated in onCreate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多