Crash when implementing tabs and swipeable views

Asked

Viewed 248 times

0

I’m starting in android development and want to put tabs with swipeable views, I followed a tutorial from Androidhive why of google tutorial It didn’t make any sense to me. The point is that after doing Swipe my application is crashing, I tried to understand the logcat but did not find the error. Follow code and logcat.

Logcat

07-11 13:46:24.218: E/AndroidRuntime(10550): FATAL EXCEPTION: main
07-11 13:46:24.218: E/AndroidRuntime(10550): Process: com.myeventsgw2, PID: 10550
07-11 13:46:24.218: E/AndroidRuntime(10550): java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1053)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at com.myeventsgw2.MainActivity.onTabSelected(MainActivity.java:72)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1105)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:547)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.view.View.performClick(View.java:4445)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.view.View$PerformClick.run(View.java:18429)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.os.Handler.handleCallback(Handler.java:733)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.os.Handler.dispatchMessage(Handler.java:95)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.os.Looper.loop(Looper.java:136)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at android.app.ActivityThread.main(ActivityThread.java:5001)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at java.lang.reflect.Method.invoke(Native Method)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-11 13:46:24.218: E/AndroidRuntime(10550):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

Mainactivity

package com.myeventsgw2;

import com.myeventsgw2.adapter.TabsPagerAdapter;
import com.myeventsgw2.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Top Rated", "Games", "Movies" };

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

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}

Tabspagera pter

package com.myeventsgw2.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.myeventsgw2.GamesFragment;
import com.myeventsgw2.MoviesFragment;
import com.myeventsgw2.TopRatedFragment;

public class TabsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {
        switch (index) {
        case 0:
            // Top rated
            return new TopRatedFragment();
        case 1:
            // second
            return new GamesFragment();
        case 3:
            // third
            return new MoviesFragment();
        }
        return null;
    }

    @Override
    public int getCount() {
        //number of tabs
        return 3;
    }
}

Topratedfragment (example of tab)

package com.myeventsgw2;

import com.myeventsgw2.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TopRatedFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_top_rated,
                container, false);

        return rootView;
    }
}

1 answer

1


Error is in your method getItem. Your Adapter has 3 Fragments correct? But by doing the switch, is skipping index 2 and going to the 3 that does not exist.

Logo on index 2 it returns null generating the Exception.

A small hit should fix this problem:

@Override
public Fragment getItem(int index) {
    switch (index) {
        case 0:
            // Top rated
            return new TopRatedFragment();
        case 1:
            // second
            return new GamesFragment();
        case 2:
            // third
            return new MoviesFragment();
    }

    return null;
}
  • My God! Silly, I was even ashamed! I think it was despair...

  • Normal, after a while are so many details that it becomes complicated to look at each one in detail.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.