-1

I'm creating an android app that was working fine until now. The app is supposed to show a list of movies and tv episodes. I'm using viewpager in this app.

I had created a boolean isMoviesSelected, now, by default, it's value is true. When the user selects the 'tv' option through the menu it's value will change to false.

So, in the viewpager, I have a if-else condition, if the isMoviesSelected is set to true, the viewpager will have 5 different tabs, each of them having a different set of data (the app was working fine for the viewpager before the if-else condition). If the value of the variable is false, the viewpager will have 4 tabs and each of them will display different data.

Also, the data to be displayed in the viewpager originally was like:

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

I changed even this so that it will send some data in string format to the fragment before opening it. For this I declared Fragment fragment = new movieFragment(); and now am using this fragment to add a bundle as argument.

But, now the app breaks down before opening. Also, this time the logcat error doesn't point any line of the code as the one causing the error, but points to my root folder, I don't have any idea what to do I am new to android developing.... Please help:

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)

Edit: Here is the bundle part

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;
            }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rem
  • 75
  • 1
  • 11

2 Answers2

1

try the following code in your Adapter:

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;
    }
}

Also overwrite the getCount() method to return number of fragment:

@Override
public int getCount() {
    return 1;    //as u have 1 fragment
}
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
1

Your getCount() method (that you have not shown) determines the cases that get hit in getItem().

If none are hit, you have the code returning null, and that is probably the error.

if the isMoviesSelected is set to true, the viewpager will have 5 different tabs, each of them having a different set of data (the app was working fine for the viewpager before the if-else condition). If the value of the variable is false, the viewpager will have 4 tabs and each of them will display different data

So, you would need

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

When you added the condition in getItem(), you have code that is probably doing this

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!
}

One fix would be to move the if (isMoviesSelected) check inside of a single case to simplify your error handling

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245