在发帖之前,我检查了很多问题,但他们没有帮助我。大多数答案都说ViewPager的布局不同,所以这就是它生成空指针异常的原因。
下面是我的stacktrace
E/AndroidRuntime: FATAL EXCEPTION: main Process: pdfshare.hemanthreddy.com.pdfshare, PID: 29092
java.lang.RuntimeException: Unable to start activity ComponentInfo{pdfshare.hemanthreddy.com.pdfshare/pdfshare.hemanthreddy.com.pdfshare.activities.HomeScreen}: 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:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
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 pdfshare.hemanthreddy.com.pdfshare.activities.HomeScreen.onCreate(HomeScreen.java:39)
at android.app.Activity.performCreate(Activity.java:6259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
这是我已声明xml
的{{1}}文件
的 activity_home_screen.xml
Viewpager
活动类 HomeScreen.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="pdfshare.hemanthreddy.com.pdfshare.activities.HomeScreen">
<android.support.v4.view.ViewPager
android:id="@+id/viewpagerhome"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
E / obj:不为空 E / pager:null 不知道为什么寻呼机对象为空。 请帮帮我,我试图在viewPager中加载4个片段,谢谢。
答案 0 :(得分:-2)
这意味着MyPagerAdapter类型的变量obj为null,返回值有错误。
MyPagerAdapter obj = new MyPagerAdapter(getSupportFragmentManager());
该行是问题,问题可能在您的自定义适配器或您的获取支持片段管理器中。如果他们正在工作,obj将不会为空。
我打赌如果你注释掉那条线和setadapter线它不会再崩溃,除非你有更大的问题