Android:这个无用的logcat错误是什么意思?

时间:2017-03-10 15:38:31

标签: android nullpointerexception android-viewpager

我正在创建一个到目前为止工作正常的Android应用程序。该应用程序应该显示电影和电视剧集列表。我在这个应用程序中使用viewpager。

我创建了一个布尔isMoviesSelected,现在,默认情况下,它的值为true。当用户通过菜单选择“tv”选项时,它的值将变为false。

所以,在viewpager中,我有一个if-else条件,如果isMoviesSelected设置为true,则viewpager将有5个不同的选项卡,每个选项卡都有不同的数据集(应用程序是在if-else条件之前为viewpager工作正常)。如果变量的值为false,则viewpager将有4个选项卡,每个选项卡将显示不同的数据。

此外,最初在viewpager中显示的数据如下:

switch(itemPosition)    {
    case 0:
      return new movieFragment();
    ......

我甚至改变了这一点,以便它在打开之前以字符串格式向片段发送一些数据。为此,我声明Fragment fragment = new movieFragment();,现在使用此fragment添加一个包作为参数。

但是,现在应用程序在打开之前就崩溃了。另外,这次logcat错误没有指出代码的任何一行是导致错误的那一行,而是指向我的根文件夹,我不知道该做什么我是android开发的新手....请帮忙:

FATAL EXCEPTION: main
                                                                            Process: com.example.android.jsontutorial, PID: 8304
                                                                            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:1248)
                                                                                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:19730)
                                                                                at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                                at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                                at android.view.View.measure(View.java:19730)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
                                                                                at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:786)
                                                                                at android.view.View.measure(View.java:19730)
                                                                                at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
                                                                                at android.view.View.measure(View.java:19730)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                                at android.view.View.measure(View.java:19730)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                at android.view.View.measure(View.java:19730)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                at android.view.View.measure(View.java:19730)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                                at android.view.View.measure(View.java:19730)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                                at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
                                                                                at android.view.View.measure(View.java:19730)
                                                                                at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)
                                                                                at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)
                                                                                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)
                                                                                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
                                                                                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
                                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                                                                                at android.view.Choreographer.doFrame(Choreographer.java:619)
                                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

编辑:这是捆绑部分

public Fragment getItem(int position) {
        if (isMoviesSelected == true) {

            Fragment fragment;
            Bundle bundle = new Bundle();
            fragment = new Movie_Fragment();

            switch (position) {
                case 0:
                    bundle.putString("movieURL", latestMovie);
                    fragment.setArguments(bundle);
                    return fragment;
                default:
                    return null;
            }

2 个答案:

答案 0 :(得分:1)

在适配器中尝试以下代码:

public Fragment getItem(int position) {
        switch (position) {
            case 0:
                Fragment fragment = new Movie_Fragment();
                if (isMoviesSelected == true) {
                    Bundle bundle = new Bundle();
                    bundle.putString("movieURL", latestMovie);
                    fragment.setArguments(bundle);
                }
                return fragment;
            default:
                return null;
    }
}

还会覆盖getCount()方法以返回片段数:

@Override
public int getCount() {
    return 1;    //as u have 1 fragment
}

答案 1 :(得分:1)

您的getCount()方法(您尚未显示)确定getItem()中遇到的案例。

如果没有命中,则代码返回null,这可能是错误。

  

如果isMoviesSelected设置为true,则viewpager将有5个不同的选项卡,每个选项卡都有不同的数据集(在if-else条件之前,app对于viewpager工作正常)。如果变量的值为false,则viewpager将有4个选项卡,每个选项卡将显示不同的数据

所以,你需要

@Override
public int getCount() {
    return movieSelected ? 5 : 4; // 5 tabs if true, else 4
}

当您在getItem()中添加条件时,您的代码可能正在执行此操作

if (movieSelected) {
    switch (position) {
        case 0:
            return ... // some fragment
        default:
            return null; // Will error if more positions than position=0
    }
} else {
    return null; // What if a movie is not selected? --> Error!
}

一种解决方法是将if (isMoviesSelected)检查移到单个case内以简化错误处理