Search code examples
androidtabsswipe

Android Tabs activity stops when opened


Hey I'm creating an android app and would like to have a simple tab layout as one of the screens. I press a button from the main activity which opens this activity.

I'm following this tutorial to the letter: http://www.lucazanini.eu/2012/android/tabs-and-swipe-views/?lang=en, and it works fine on its own but when I incorporate it into my app it stops and exits.

This is the java file:

package ie.workinprogress.turas;


import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class IrelandInfo extends FragmentActivity implements
    ActionBar.TabListener {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the three primary sections of the app. We use a
     * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
     * will keep every loaded fragment in memory. If this becomes too memory
     * intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    CollectionPagerAdapter mCollectionPagerAdapter;

    /**
     * The {@link android.support.v4.view.ViewPager} that will display the
     * object collection.
     */
    ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ireland_info);

    // Create an adapter that when requested, will return a fragment
    // representing an object in
    // the collection.
    //
    // ViewPager and its adapters use support library fragments, so we must
    // use
    // getSupportFragmentManager.
    mCollectionPagerAdapter = new CollectionPagerAdapter(
        getSupportFragmentManager());

    // Set up action bar.
    final ActionBar actionBar = getActionBar();

    // Specify that the Home/Up button should not be enabled, since there is
    // no hierarchical
    // parent.
    actionBar.setHomeButtonEnabled(false);

    // Specify that we will be displaying tabs in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager, attaching the adapter and setting up a listener
    // for when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);
    mViewPager
        .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
            // When swiping between different app sections, select
            // the corresponding tab.
            // We can also use ActionBar.Tab#select() to do this if
            // we have a reference to the
            // Tab.
            actionBar.setSelectedNavigationItem(position);
            }
        });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter.
        // Also specify this Activity object, which implements the
        // TabListener interface, as the
        // listener for when this tab is selected.
        actionBar.addTab(actionBar.newTab()
            .setText(mCollectionPagerAdapter.getPageTitle(i))
            .setTabListener(this));
    }
    }

    public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }

    public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
    }

    public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the primary sections of the app.
     */
    public class CollectionPagerAdapter extends FragmentPagerAdapter {

    final int NUM_ITEMS = 3; // number of tabs

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

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new TabFragment();
        Bundle args = new Bundle();
        args.putInt(TabFragment.ARG_OBJECT, i);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        String tabLabel = null;
        switch (position) {
        case 0:
        tabLabel = getString(R.string.tab1);
        break;
        case 1:
        tabLabel = getString(R.string.tab2);
        break;
        case 2:
        tabLabel = getString(R.string.tab3);
        break;
        }

        return tabLabel;
    }
    }

    /**
     * A fragment that launches other parts of the demo application.
     */
    public static class TabFragment extends Fragment {

    public static final String ARG_OBJECT = "object";

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

        Bundle args = getArguments();
        int position = args.getInt(ARG_OBJECT);

        int tabLayout = 0;
        switch (position) {
        case 0:
        tabLayout = R.layout.tab1;
        break;
        case 1:
        tabLayout = R.layout.tab2;
        break;
        case 2:
        tabLayout = R.layout.tab3;
        break;
        }

        View rootView = inflater.inflate(tabLayout, container, false);

        return rootView;
    }
    }

}

LogCat tells me the error is on line 5 caused by a java.lang.NullPointerException.

final ActionBar actionBar = getActionBar();

I'm new to Android and I'm not too sure about how to go about solving it. Any advice would be much appreciated

EDIT: Crash Log

12-28 16:08:15.650: E/Trace(2815): error opening trace file: No such file or directory (2)
12-28 16:08:22.560: E/AndroidRuntime(2815): FATAL EXCEPTION: main
12-28 16:08:22.560: E/AndroidRuntime(2815): java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.workinprogress.turas/ie.workinprogress.turas.IrelandInfo}: java.lang.NullPointerException
12-28 16:08:22.560: E/AndroidRuntime(2815):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at android.os.Looper.loop(Looper.java:137)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at java.lang.reflect.Method.invoke(Method.java:511)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at dalvik.system.NativeStart.main(Native Method)
12-28 16:08:22.560: E/AndroidRuntime(2815): Caused by: java.lang.NullPointerException
12-28 16:08:22.560: E/AndroidRuntime(2815):     at ie.workinprogress.turas.IrelandInfo.onCreate(IrelandInfo.java:55)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at android.app.Activity.performCreate(Activity.java:5008)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-28 16:08:22.560: E/AndroidRuntime(2815):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-28 16:08:22.560: E/AndroidRuntime(2815):     ... 11 more
12-28 16:12:15.720: E/Trace(2904): error opening trace file: No such file or directory (2)
12-28 16:12:24.020: E/AndroidRuntime(2904): FATAL EXCEPTION: main
12-28 16:12:24.020: E/AndroidRuntime(2904): java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.workinprogress.turas/ie.workinprogress.turas.IrelandInfo}: java.lang.NullPointerException
12-28 16:12:24.020: E/AndroidRuntime(2904):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at android.os.Looper.loop(Looper.java:137)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at java.lang.reflect.Method.invoke(Method.java:511)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at dalvik.system.NativeStart.main(Native Method)
12-28 16:12:24.020: E/AndroidRuntime(2904): Caused by: java.lang.NullPointerException
12-28 16:12:24.020: E/AndroidRuntime(2904):     at ie.workinprogress.turas.IrelandInfo.onCreate(IrelandInfo.java:55)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at android.app.Activity.performCreate(Activity.java:5008)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-28 16:12:24.020: E/AndroidRuntime(2904):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-28 16:12:24.020: E/AndroidRuntime(2904):     ... 11 more

EDIT 2: Layout File

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

styles.xml:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

Solution

  • In the end I went back and created the project again and selected the "Holo Light with Dark Action Bar" Theme at the New Android Application screen (in Eclipse). After this I went through the tutorial again and copied in the code from the other activities. I think I originally picked too low an API for the app (I needed higher for animations and other things) and a base theme that didn't include the action bar.

    Thanks for all the help :)