在PagerTabStrip中添加图标

时间:2016-01-11 09:57:07

标签: android android-viewpager pagertabstrip

我尝试在PagerTabStrip中添加图像和文本,但是获取错误和应用程序是强制关闭。我正在使用here中的说明为ViewPager的PagerTabStrip显示。在this帖子中,它显示了如何添加图片,但它不起作用,代码不完整。所以,任何帮助将不胜感激。我正在使用PagerTabStrip becuz我想让我的标签从中心开始。

LogCat error

01-11 15:15:47.684: E/AndroidRuntime(7583): FATAL EXCEPTION: main
01-11 15:15:47.684: E/AndroidRuntime(7583): Process: in.wptrafficanalyzer.viewpagerdemo, PID: 7583
01-11 15:15:47.684: E/AndroidRuntime(7583): java.lang.NullPointerException
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at in.wptrafficanalyzer.viewpagerdemo.MyFragmentPagerAdapter.getPageTitle(MyFragmentPagerAdapter.java:45)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.support.v4.view.PagerTitleStrip.updateText(PagerTitleStrip.java:276)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.support.v4.view.PagerTitleStrip.updateAdapter(PagerTitleStrip.java:321)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.support.v4.view.PagerTitleStrip.onAttachedToWindow(PagerTitleStrip.java:252)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.View.dispatchAttachedToWindow(View.java:12851)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2579)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2586)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2586)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2586)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2586)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2586)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1427)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:816)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.Choreographer.doCallbacks(Choreographer.java:619)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.Choreographer.doFrame(Choreographer.java:588)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:802)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.os.Handler.handleCallback(Handler.java:808)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.os.Handler.dispatchMessage(Handler.java:103)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.os.Looper.loop(Looper.java:193)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at android.app.ActivityThread.main(ActivityThread.java:5388)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at java.lang.reflect.Method.invokeNative(Native Method)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at java.lang.reflect.Method.invoke(Method.java:515)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
01-11 15:15:47.684: E/AndroidRuntime(7583): 	at dalvik.system.NativeStart.main(Native Method)
Adapter

package in.wptrafficanalyzer.viewpagerdemo;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ImageSpan;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
	
	final int PAGE_COUNT = 3;
	Drawable myDrawable;

	/** Constructor of the class */
	public MyFragmentPagerAdapter(FragmentManager fm) {
		super(fm);
	}

	/** This method will be invoked when a page is requested to create */
	@Override
	public Fragment getItem(int arg0) {
		
		MyFragment myFragment = new MyFragment();
		Bundle data = new Bundle();
		data.putInt("current_page", arg0+1);
		myFragment.setArguments(data);
		return myFragment;
	}

	/** Returns the number of pages */
	@Override
	public int getCount() {		
		return PAGE_COUNT;
	}
	
	@Override
	public CharSequence getPageTitle(int position) {		
		//return "Page #" + ( position + 1 );
		
		SpannableStringBuilder sb = new SpannableStringBuilder(" Page #"+ position); // space added before text for convenience

	    myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight()); 
	    ImageSpan span = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE); 
	    sb.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

	    return sb;
	}
	
	
	
	
}

0 个答案:

没有答案