我正在尝试使用PagerTabStrip将Jake Wharton的ViewPagerIndicator应用到我已经工作的,功能齐全的应用程序中。我按照git repo上的说明删除了对PagerTabStrip的调用。
我将库添加到我的工作区并将其作为库添加到我的应用程序中。 我的main_activity代码如下所示:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
vpPager.setAdapter(adapterViewPager);
TabPageIndicator mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(vpPager);
当我的代码到达mIndicator.setViewPager(vpPager)时,我得到一个RunTimeException,NullPointerException;这是因为mIndicator为null。我假设这意味着findViewById无效,但我的资源xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/indicator"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<android.support.v4.view.ViewPager
android:id="@+id/vpPager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
</LinearLayout>
一切似乎都是正确的,但我不确定为什么这不起作用。
我的logcat显示了这个:
03-19 00:10:15.201: E/AndroidRuntime(1486): FATAL EXCEPTION: main
03-19 00:10:15.201: E/AndroidRuntime(1486): java.lang.RuntimeException: Unable to start activity ComponentInfo{com./////.MainActivity}: java.lang.NullPointerException
03-19 00:10:15.201: E/AndroidRuntime(1486): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-19 00:10:15.201: E/AndroidRuntime(1486): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-19 00:10:15.201: E/AndroidRuntime(1486): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-19 00:10:15.201: E/AndroidRuntime(1486): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-19 00:10:15.201: E/AndroidRuntime(1486): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 00:10:15.201: E/AndroidRuntime(1486): at android.os.Looper.loop(Looper.java:137)
03-19 00:10:15.201: E/AndroidRuntime(1486): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-19 00:10:15.201: E/AndroidRuntime(1486): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 00:10:15.201: E/AndroidRuntime(1486): at java.lang.reflect.Method.invoke(Method.java:525)
03-19 00:10:15.201: E/AndroidRuntime(1486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-19 00:10:15.201: E/AndroidRuntime(1486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-19 00:10:15.201: E/AndroidRuntime(1486): at dalvik.system.NativeStart.main(Native Method)
03-19 00:10:15.201: E/AndroidRuntime(1486): Caused by: java.lang.NullPointerException
03-19 00:10:15.201: E/AndroidRuntime(1486): at com.////.MainActivity.onCreate(MainActivity.java:47)
03-19 00:10:15.201: E/AndroidRuntime(1486): at android.app.Activity.performCreate(Activity.java:5133)
03-19 00:10:15.201: E/AndroidRuntime(1486): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-19 00:10:15.201: E/AndroidRuntime(1486): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-19 00:10:15.201: E/AndroidRuntime(1486): ... 11 more
谢谢,我希望有人可以提供帮助!
注意:在日志中我用////替换了我的com信息,所以这不是日志中的错误。