Issue
So I have tried various solutions to this problem. Here is an example of where I started:
class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
SimpleFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
private String tabTitles[] = new String[] {"one","two","three","four"};
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
Then I tried the following to get the same strings from the string.xml:
class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
SimpleFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
private String tabTitles[] = new String[] {Resources.getSystem().getString(R.string.one),
Resources.getSystem().getString(R.string.two),
Resources.getSystem().getString(R.string.three),
Resources.getSystem().getString(R.string.four)};
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
But I received this error: FATAL EXCEPTION: main Process: com.example.android.myapp, PID: 9923 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.myapp/com.example.android.myapp.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x7f070017
Since I know the strings exist and I've used them in other areas of my code I thought that it might need the context, so I passed MainActivity.this through the adapter and tried:
class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
private Context mContext;
SimpleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.mContext = context;
}
private String tabTitles[] = new String[] {mContext.getResources().getString(R.string.one),
mContext.getResources().getString(R.string.two),
mContext.getResources().getString(R.string.three),
mContext.getResources().getString(R.string.four)};
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
And received this error: FATAL EXCEPTION: main Process: com.example.android.myapp, PID: 5387 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.myapp/com.example.android.myapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Finally, I tried to convert the resources to string within the MainActivity and pass them through to the adapter:
class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
private String mOne;
private String mTwo;
private String mThree;
private String mFour;
SimpleFragmentPagerAdapter(FragmentManager fm, one, two, three, four) {
super(fm);
mOne = one;
mTwo = two;
mThree = three;
mFour = four;
}
private String tabTitles[] = new String[] {mOne,mTwo,mThree,mFour};
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
It ran without errors but simply resulted in blank tab titles.
Maybe I'm just being a complete noob here and missing something simple but I'd appreciate any help you guys could provide.
Solution
Look at these lines of code:
private String tabTitles[] = new String[] {mOne,mTwo,mThree,mFour};
private String tabTitles[] = new String[] {
mContext.getResources().getString(R.string.one),
mContext.getResources().getString(R.string.two),
mContext.getResources().getString(R.string.three),
mContext.getResources().getString(R.string.four)
};
The problem is, these arrays are initialized with empty (actually, null) values of mOne, mTwo, mThree and mFour. Only after that, you are assigning them values in your constructor. But this will not update your array. Look at this example:
class SimpleFragmentPagerAdapter extends FragmentPagerAdapter {
private Context mContext;
private String tabTitles[];
SimpleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.mContext = context;
tabTitles = new String[]{
mContext.getResources().getString(R.string.one),
mContext.getResources().getString(R.string.two),
mContext.getResources().getString(R.string.three),
mContext.getResources().getString(R.string.four)
};
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
What I've done here is saying "Hey Java, there is a String array called tabTitles, which is not initialized (it has null value)." And then, in the constructor, "Hey Java, assign these values to array called tabTitles". Now, when getPageTitle(int position) is called,
Answered By - MatusMak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.