在AndroidAnnotations之后,ViewPager发送NullPointerException

时间:2016-04-12 00:18:58

标签: java android xml nullpointerexception android-viewpager

美好的一天。我已将AndroidAnnotations添加到现有项目中。在添加插件之前,它就像一个魅力。

之后,我在ViewPager上收到NullPointerException。

这是我的 main_activity.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primary"
    android:orientation="vertical">

<android.support.v4.view.ViewPager
    android:id="@+id/main_act_vp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<Button
    android:id="@+id/main_act_btn"
    android:layout_marginBottom="55dp"
    android:layout_marginRight="100dp"
    android:layout_marginLeft="100dp"
    android:layout_width="match_parent"
    android:layout_height="58dp"
    android:textColor="@color/white"
    android:textSize="14sp"
    android:gravity="center"
    android:background="@drawable/button_border"
    android:text="@string/description_button_knowmore"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

这是我的 MainActivity ,我正在注入ViewPager:

@EActivity(R.layout.main_activity)
public class MainActivity extends FragmentActivity {

    ....

    @ViewById(R.id.main_act_vp)
    ViewPager main_activity_viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FragmentStatePagerAdapter main_act_adapter =
            new FragmentStatePagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new ObFirst_();
                case 1:
                    return new ObSecond_();
                case 2:
                    return new ObThird_();
                default:
                    return null;
            }
        }
        @Override
        public int getCount() {
            return 3;
        }
    };

    main_activity_viewPager.setAdapter(main_act_adapter);

    ....
}

这是我得到的错误

FATAL EXCEPTION: main
Process: com.avena.avenafit, PID: 14218
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.avena.avenafit/com.avena.avenafit.onboarding.MainActivity_}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference at     com.avena.avenafit.onboarding.MainActivity.onCreate(MainActivity.java:148)
at com.avena.avenafit.onboarding.MainActivity_.onCreate(MainActivity_.java:39)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

你们认为这可能是什么问题?

修改

以下评论间接地给了我答案。谢谢。

0 个答案:

没有答案