2015-07-03 82 views
3

我試圖在我的SlidingTabLayout上顯示圖標,所以在我的適配器中我創建了這樣的東西,我發現它有時,當我在找教程,我編輯在這裏您將使用getDrawable,因爲它說,它已經過時和應用,我發現android.content.res.Resources android.content.Context.getResources()'null對象引用

@Override 
public CharSequence getPageTitle(int position) { 

    Drawable image = ResourcesCompat.getDrawable(mContext.getResources(), icons[position], null); 
    image.setBounds(0, 0, 48, 48); 
    SpannableString sb = new SpannableString(" "); 
    ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM); 
    sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    return sb; 
} 

我也聲明這是我的適配器在哪裏我已檢查解決方案的第一部分真的把它放在我的drawable上,它全部都在那裏。

private int[] icons = { 
     R.drawable.tabone, 
     R.drawable.tabtwo, 
     R.drawable.tabthree, 
     R.drawable.tapfour, 
     R.drawable.tabfive 
}; 

我不知道我做了什麼錯誤的,但即使我聲明一個變量來處理圖像哪裏總是有這個錯誤

Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 

。任何人都可以幫我請我的最小版本是14,我的目標SDK是22.謝謝你提前。

這裏是我爲我的適配器

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 

CharSequence Titles[]; 
int NumbOfTabs; 

Context mContext; 

private int[] icons = { 
     R.drawable.tabone, 
     R.drawable.tabtwo, 
     R.drawable.tabthree, 
     R.drawable.tapfour, 
     R.drawable.tabfive 
}; 


public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb, Context context) { 
    super(fm); 

    this.Titles = mTitles; 
    this.NumbOfTabs = mNumbOfTabsumb; 
    this.mContext = context; 

} 


@Override 
public Fragment getItem(int position) { 

    if(position == 0) // if the position is 0 we are returning the First tab 
    { 
     Tabone tab_one = new Tabone(); 
     return tab_one; 
    } 
    else if(position == 1) 
    { 
     Tabtwo tab_two = new Tabtwo(); 
     return tab_two; 

    }else if(position == 2){ 
     Tabthree tab_three = new Tabthree(); 
     return tab_three; 
    }else if(position == 3){ 
     Tabfour tab_four = new Tabfour(); 
     return tab_four; 
    }else{ 
     Tabfive tab_five = new Tabfive(); 
     return tab_five; 
    } 

} 


@Override 
public CharSequence getPageTitle(int position) { 

    Drawable image = ResourcesCompat.getDrawable(mContext.getResources(), icons[position], null); 
    image.setBounds(0, 0, 48, 48); 
    SpannableString sb = new SpannableString(" "); 
    ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM); 
    sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    return sb; 
} 



// This method return the Number of tabs for the tabs Strip 

@Override 
public int getCount() { 
    return NumbOfTabs; 
} 

}

這裏是我的MainActivity

private ViewPager mPager; 
private ViewPagerAdapter adapter; 
private SlidingTabLayout mTabs; 
private Context mContext; 

CharSequence Titles[] = {"One", "Two", "Three", "Four", "Five"}; 

int Numboftabs = 5; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, mContext); 

    // Assigning ViewPager View and setting the adapter 
    mPager = (ViewPager) findViewById(R.id.pager); 
    mPager.setAdapter(adapter); 

    // Assigning the Sliding Tab Layout View 
    mTabs = (SlidingTabLayout) findViewById(R.id.tabs); 
    mTabs.setDistributeEvenly(true); 


    // Setting the ViewPager For the SlidingTabsLayout 
    mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.tabsIndicator)); 
    mTabs.setViewPager(mPager); 
} 
+0

mContext一世s null? – rainash

+0

您的mContect爲空,您可以發佈整個代碼嗎? –

+0

我把上面的代碼 –

回答

3

在這裏看到代碼:

adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, mContext); 

從這裏mContext是去null。因爲您沒有在onCreate方法中分配任何參考mContext變量。

將其更改爲:

adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, MainActivity.this); 

,或者你也可以試試這個:

onCreate方法:

mContext = MainActivity.this; 

adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs, mContext); 

使用此代碼ViewPagerAdapter

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 

CharSequence Titles[]; 
int NumbOfTabs; 

private Activity mContext; 

private int[] icons = { 
     R.drawable.tabone, 
     R.drawable.tabtwo, 
     R.drawable.tabthree, 
     R.drawable.tapfour, 
     R.drawable.tabfive 
}; 


public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb, Activity context) { 
    super(fm); 

    this.Titles = mTitles; 
    this.NumbOfTabs = mNumbOfTabsumb; 
    this.mContext = context; 

} 


@Override 
public Fragment getItem(int position) { 

    if(position == 0) // if the position is 0 we are returning the First tab 
    { 
     Tabone tab_one = new Tabone(); 
     return tab_one; 
    } 
    else if(position == 1) 
    { 
     Tabtwo tab_two = new Tabtwo(); 
     return tab_two; 

    }else if(position == 2){ 
     Tabthree tab_three = new Tabthree(); 
     return tab_three; 
    }else if(position == 3){ 
     Tabfour tab_four = new Tabfour(); 
     return tab_map; 
    }else{ 
     Tabfive tab_five = new Tabfive(); 
     return tab_five; 
    } 

} 


    @Override 
public CharSequence getPageTitle(int position) { 
    Drawable image = mContext.getResources().getDrawable(icons[position]); 
    image.setBounds(0, 0, 48, 48); 
    SpannableString sb = new SpannableString(" "); 
    ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM); 
    sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    return sb; 
} 





// This method return the Number of tabs for the tabs Strip 

@Override 
public int getCount() { 
    return NumbOfTabs; 
} 
+0

它現在工作,但我的圖標沒有顯示:( –

+0

@KairiSan嘗試我的代碼'ViewPagerAdapter' –

+0

我試過的代碼,但它仍然沒有顯示圖標:(但我現在沒有任何錯誤 –

相關問題