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

问题: I have tried unsuccessfully to pass the context of SimpleFragmentPageAdapter via its constructor to the MainActivity, calling getResources().getString() on this context var...

问题:

I have tried unsuccessfully to pass the context of SimpleFragmentPageAdapter via its constructor to the MainActivity, calling getResources().getString() on this context variable, in order to retrieve the titles for each tab in the page adapter.

Any help is much appreciated!

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>

Relevant error log for reference:

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

回答1:

You try to access context that is not initialized yet.

Make it like this:

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])
 }

Or better:

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])
 }

回答2:

context is null until SimpleFragmentPageAdapter is initialized, you need to move the variable's initialization inside the constructor:

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};
    }
}

回答3:

There is another solution: your pageAdapter don't really need to hold the context by itself.

You want to access the string, and why not you create a global static context to do this job? Usually I will make one from my Application instance, thus I can use the context to get resource from every where I want.

Another advantage of this approach is, you can avoid memory leak. Those life cycle related component don't need to hold the context by themself, the context from Application is safe to use while the application is alive.

  • 发表于 2018-07-06 05:51
  • 阅读 ( 500 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除