ViewPager适配器上的空例外 - Android

时间:2017-04-18 15:37:35

标签: android android-studio

我正在尝试使用我的适配器在Android中实现ViewPager并收到此错误,我已经使用Google搜索,一切似乎都很好,但我的应用仍在崩溃,任何帮助都将受到高度赞赏。

适配器:

public class MainPagerAdapter extends FragmentPagerAdapter {

public MainPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch (position){
        case 0:
            ChatFragment.create();
        case 1:
            EmptyFragment.create();
        case 2:
            StoryFragment.create();
    }
    return  null;
}

@Override
public int getCount() {
    return 3;
}

}

MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    View back = (View) findViewById(R.id.am_background_view);
    ViewPager pager = (ViewPager) findViewById(R.id.am_view_pager);
    MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager());
    pager.setAdapter(adapter);
}

}

MainActivity.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<View
    android:id="@+id/am_background_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/black"
    />




<android.support.v4.view.ViewPager
    android:id="@+id/am_view_pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

ChatFragment

public class ChatFragment extends Fragment {

public  static ChatFragment create() {
    return new ChatFragment();
}


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_chat, container, false);
    return view;
}

错误:

FATAL EXCEPTION: main
                                                                    Process: pk.fastech.snappy.snappy, PID: 6148
                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                                                                        at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:380)
                                                                        at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:375)
                                                                        at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:103)
                                                                        at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
                                                                        at android.support.v4.view.ViewPager.populate(ViewPager.java:1182)
                                                                        at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
                                                                        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
                                                                        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                                                                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
                                                                        at android.view.View.measure(View.java:20158)
                                                                        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2644)
                                                                        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1599)
                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1891)
                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1487)
                                                                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7450)
                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:695)
                                                                        at android.view.Choreographer.doFrame(Choreographer.java:631)
                                                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:158)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

1 个答案:

答案 0 :(得分:1)

您的getItem方法不正确,并始终返回null

您应将其更改为:

@Override
public Fragment getItem(int position) {
    switch (position){
        case 0:
            return ChatFragment.create();
        case 1:
            return EmptyFragment.create();
        case 2:
            return StoryFragment.create();
    }
    return null;
}
Java中的

return将结果返回给调用方法 - 但是你没有返回这个结果 - 它没有“堆栈”。