2013-12-16 77 views
1

我試圖從擴展PagerAdapter的類中xml資源中獲取一個字符串數組。我嘗試過各種構造函數,但沒有運氣。即NullPointerExceptions任何幫助? 這是我的適配器:在PagerAdapter中獲取資源

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變量仍爲空。我需要幫助初始化它

+0

分享一些代碼。開發人員瞭解並解決您的問題。 –

+0

我已經添加了代碼。 –

+0

Hello Innen Tensai,您的CONTENT在哪裏申報? –

回答

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]; 
} 
} 

的作品就像一個夢:-)謝謝!