我正在尝试从扩展PagerAdapter的类中的xml资源中获取字符串数组。我尝试了各种没有运气的构造函数。即NullPointerExceptions任何帮助? 这是我的适配器:
class CharacterViewPagerAdapter extends PagerAdapter {
CharacterView activity;
public Object mPagerAdapter(Context context) {
Resources res = context.getResources();
CONTENT = res.getStringArray(R.array.heroes);
return CONTENT;
}
public int getCount() {
return CONTENT.length;
}
public CharacterViewPagerAdapter(CharacterView activity) {
this.activity = activity;
}
public Object instantiateItem(final View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = R.layout.character_profile;
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
activity.initPagerView(position, view);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public CharSequence getPageTitle(int position) {
return CharacterViewPagerAdapter.CONTENT[position % CONTENT.length];
}
}
CONTENT变量仍为null。我需要初始化帮助
答案 0 :(得分:0)
我已经这样解决了:
class CharacterViewPagerAdapter extends PagerAdapter {
CharacterView activity;
static Context context;
static Resources res = null;
static String[] CONTENT = null;
public int getCount() {
return CONTENT.length;
}
public CharacterViewPagerAdapter(CharacterView activity, Context c) {
this.activity = activity;
context = c;
res = context.getResources();
ArrayList<String> CONTENT = new ArrayList<String>();
CONTENT = res.getStringArray(R.array.CONTENT);
Collections.addAll(CONTENT, CONTENT);
Collections.sort(CONTENT);
CONTENT = new String[CONTENT.size()];
CONTENT = CONTENT.toArray(CONTENT);
}
public Object instantiateItem(final View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = R.layout.character_profile;
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
activity.initPagerView(position, view);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public CharSequence getPageTitle(int position) {
return CharacterViewPagerAdapter.CONTENT[position % CONTENT.length];
}
}
像梦一样:-)谢谢!