我尝试将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
答案 0 :(得分:1)
context
之前, null
为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};
}
}
答案 1 :(得分:0)
您尝试访问尚未初始化的上下文。
像这样:
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])
}
答案 2 :(得分:0)
还有另一种解决方案:您的pageAdapter确实不需要自己保存上下文。
您想访问该字符串,为什么不创建一个全局静态上下文来完成此工作呢?通常,我会从Application
实例中创建一个实例,这样我就可以使用上下文从我想要的每个位置获取资源。
此方法的另一个优点是,可以避免内存泄漏。那些与生命周期相关的组件不需要自己保存上下文,Application
中的上下文在应用程序运行时可以安全使用。