我正在尝试使用我的应用程序实现ViewPager,因此我可以在2个选项卡之间滑动,这两个选项卡应该是2个不同的片段。这两个选项卡是ADD选项卡和LIST选项卡。用户在ADD选项卡中填写信息,它将显示在LIST选项卡中。它与TabHost完美配合,但是现在我试图使用ViewPager它不起作用,Android工作室给我一个NPE,我的手机说“不幸......已经停止了”
在关注教程之后,它说我应该在我的activity_main.xml中放置一个ViewPager,其中以前是添加标签的UI。我创建了一个新的XML布局文件(add_fragment),并将activity_main中的UI放入add_fragment,然后将ViewPager放入activity_main。如果我将UI元素与ViewPager放在一起,ViewPager会阻止所有Ui元素,因此您只有空白背景。
问题是我的MainActivity.java,当我setContentView(R.layout.activity_main)它无法在add_fragment中找到UI元素,因此返回null但是如果我把setContentView(R.layout.add_fragment)找不到它ViewPager和我也收到了NPE。
NPE
7-07 09:31:47.870 1837-1837/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
07-07 09:31:47.870 1837-1837/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
07-07 09:31:47.871 11145-11145/com.mycompany.appointmentmanager D/AndroidRuntime﹕ Shutting down VM
07-07 09:31:47.872 11145-11145/com.mycompany.appointmentmanager E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mycompany.appointmentmanager, PID: 11145
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.appointmentmanager/com.mycompany.appointmentmanager.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
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:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.mycompany.appointmentmanager.MainActivity.showDialogOnEditTextClick(MainActivity.java:199)
at com.mycompany.appointmentmanager.MainActivity.onCreate(MainActivity.java:77)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access$900(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
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:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
activity_main
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v4.view.ViewPager>
任何帮助都会非常感激。
提前致谢,