如何创建 SimpleFragmentPageAdapter 到 getResources().getString() 的上下文?

How do I create context of SimpleFragmentPageAdapter to getResources().getString()?

我曾尝试通过其构造函数将 SimpleFragmentPageAdapter 的上下文传递给 MainActivity,并在此上下文变量上调用 getResources().getString() 以检索页面适配器中每个选项卡的标题,但未成功。

非常感谢任何帮助!

SimpleFragmentPageAdapter.java

public class SimpleFragmentPageAdapter extends FragmentPagerAdapter {

    final int PAGE_COUNT = 5;
    private Context context;

    String tabOne = context.getResources().getString(R.string.kiez);
    String tabTwo = context.getResources().getString(R.string.history);
    String tabThree = context.getResources().getString(R.string.culture);
    String tabFour = context.getResources().getString(R.string.food);
    String tabFive = context.getResources().getString(R.string.green);

    private String[] tabNames = new String [] {tabOne, tabTwo, tabThree, tabFour, tabFive};

    public SimpleFragmentPageAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        // Set view to activity:main
        setContentView(R.layout.activity_main);

        // Find ViewPager for easy swiping
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

        // Create adapater object to know which fragment should be shown based on user interaction
        SimpleFragmentPageAdapter pagerAdapter = new SimpleFragmentPageAdapter(getSupportFragmentManager(), this);

        // Set adapter to ViewPager
        viewPager.setAdapter(pagerAdapter);

        // Find TabLayout for tab identification
        TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);

        // Set ViewPager to TabLayout
        tabLayout.setupWithViewPager(viewPager);

    }
}

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"/>

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

</LinearLayout>

相关错误日志供参考:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

您尝试访问尚未初始化的上下文。

像这样:

private Context context;

private int[] tabNames = new int[] {R.string.kiez, ....};

public ViewPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    this.context = context;
}

 @Override
    public CharSequence getPageTitle(int position) {
       context.getResources().getString(tabNames[position])
 }

或更好:

private Resources resources;

private int[] tabNames = new int[] {R.string.kiez, ....};

public ViewPagerAdapter(FragmentManager fm, Resources resources) {
    super(fm);
    this.resources= resources;
}

 @Override
    public CharSequence getPageTitle(int position) {
       resources.getString(tabNames[position])
 }

contextnull 直到 SimpleFragmentPageAdapter 被初始化,你需要将变量的初始化移动到构造函数中:

public class SimpleFragmentPageAdapter extends FragmentPagerAdapter {

    final int PAGE_COUNT = 5;
    private Context context;

    String tabOne;
    String tabTwo;
    String tabThree;
    String tabFour;
    String tabFive;

    private String[] tabNames;

    public SimpleFragmentPageAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;

        tabOne = context.getResources().getString(R.string.kiez);
        tabTwo = context.getResources().getString(R.string.history);
        tabThree = context.getResources().getString(R.string.culture);
        tabFour = context.getResources().getString(R.string.food);
        tabFive = context.getResources().getString(R.string.green);

        tabNames = new String [] {tabOne, tabTwo, tabThree, tabFour, tabFive};
    }
}

还有另一种解决方案:您的 pageAdapter 并不需要自己保存上下文。

您想访问字符串,为什么不创建一个全局静态上下文来完成这项工作?通常我会从我的 Application 实例中创建一个,因此我可以使用上下文从我想要的任何地方获取资源。

这种方法的另一个好处是,可以避免内存泄漏。那些与生命周期相关的组件不需要自己保存上下文,来自 Application 的上下文在应用程序处于活动状态时可以安全使用。