我想從xml訪問字符串名稱到這個字符串數組中,但它給了我空對象錯誤,我該怎麼辦?我怎樣才能訪問這個字符串?我已經使用上下文,但它不工作,有人可以告訴我,我該怎麼做?想要訪問XML中的一個片段中的字符串
public class ViewProfileAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 4;
Context context;
String a=context.getApplicationContext().getResources().getString(R.string.viewkundli_tab_kundli);
String b=context.getApplicationContext().getResources().getString(R.string.viewkundli_tab_remedies);
String c=context.getApplicationContext().getResources().getString(R.string.viewkundli_tab_predictions);
String d=context.getApplicationContext().getResources().getString(R.string.viewkundli_tab_panchang);
private String tabTitles[] = new String[] {a,b,c,d };
public ViewProfileAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return ViewKundli_1.newInstance(position);
case 1:
return ViewKundli_2.newInstance(position);
case 2:
return ViewKundli_3.newInstance(position);
case 3:
return ViewKundli_4.newInstance(position);
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
你沒有初始化你的上下文變量。只要使用getApplicationContext()。getResources().... – X3Btel
@bipin你可以接受幫助答案咆哮任何其他人從你的貢獻獲得幫助:) – MilapTank