我尝试将值从xml array
传递给Listfragment
。
public class TabsPagerAdapter extends FragmentPagerAdapter {
static Context context;
static Resources res = null;
static String[] CONTENT = null;
public TabsPagerAdapter(FragmentManager fm, Context c) {
super(fm);
context = c;
}
@Override
public ListFragment getItem(int index) {
res = context.getResources();
CONTENT = res.getStringArray(R.array.trg_values);
ListFragment fragment = new DaerahFragment();
Bundle args = new Bundle();
args.putString("daerah", CONTENT[index] );
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 7;
}
}
这是我的xml
<string-array name="trg_values">
<item name="Kuala Terengganu">1</item>
<item name="Marang">2</item>
<item name="Dungun">3</item>
<item name="Kemaman">4</item>
<item name="Hulu Terengganu">5</item>
<item name="Setiu">6</item>
<item name="Besut">7</item>
</string-array>
错误:
08-03 16:12:00.992: E/AndroidRuntime(28017): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
08-03 16:12:00.992: E/AndroidRuntime(28017): at com.jkmt.ppbanjir.adapter.TabsPagerAdapter.getItem(TabsPagerAdapter.java:62)
适配器的实例化
mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), null);
viewPager.setAdapter(mAdapter);
答案 0 :(得分:0)
问题在于这一行
LC_ALL=en_GB.ascii python
您将null作为mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), null);
传递,并将null分配给适配器中的成员。调用getItem并引用成员时,应用程序崩溃Context
。用
NullPointerException
如果您在mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), this);
或
Activity
如果你在mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), getActivity());