1

I'm just trying to create a simple application with 3 tabs so that I can begin to get my head around actionBar tabs and fragments. I've been following this tutorial http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/" but when I go to run it on my connected device(4.4.4) then it just crashes on startup. My minSDK is 19 and compile SDK is 21. The only thing different between the code in the link and my code is that the package name is different, so the code in the link is what I am using. Any help as to why the app keeps crashing on startup will be much apreciated, thanks!

Logcat

 02-12 15:21:46.678  13813-13813/com.androidbegin.absfragtabhost E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.androidbegin.absfragtabhost, PID: 13813
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbegin.absfragtabhost/com.androidbegin.absfragtabhost.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.access$800(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.androidbegin.absfragtabhost.MainActivity.onCreate(MainActivity.java:23)
        at android.app.Activity.performCreate(Activity.java:5248)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.access$800(ActivityThread.java:139)
          atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
 atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
       at dalvik.system.NativeStart.main(Native Method)

EDIT MainActivity (same as the one in the link)

package com.androidbegin.absfragtabhost;

import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {
// Declare Tab Variable
ActionBar.Tab Tab1, Tab2, Tab3;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
Fragment fragmentTab3 = new FragmentTab3();

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

    ActionBar actionBar = getActionBar();

    // Hide Actionbar Icon
    actionBar.setDisplayShowHomeEnabled(false);

    // Hide Actionbar Title
    actionBar.setDisplayShowTitleEnabled(false);

    // Create Actionbar Tabs
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set Tab Icon and Titles
    Tab1 = actionBar.newTab().setIcon(R.drawable.tab1);
    Tab2 = actionBar.newTab().setText("Tab2");
    Tab3 = actionBar.newTab().setText("Tab3");

    // Set Tab Listeners
    Tab1.setTabListener(new TabListener(fragmentTab1));
    Tab2.setTabListener(new TabListener(fragmentTab2));
    Tab3.setTabListener(new TabListener(fragmentTab3));

    // Add tabs to actionbar
    actionBar.addTab(Tab1);
    actionBar.addTab(Tab2);
    actionBar.addTab(Tab3);
 }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Android manifest xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidbegin.absfragtabhost" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  </application>

</manifest>
user3375184
  • 147
  • 2
  • 3
  • 8

2 Answers2

1

if you want to use the ActionBar you should extend your Activity from ActionBarActivity because usual Activities don't have any ActionBar.

You also can add the new Google Toolbar to your Layout and then get it by

Toolbar toolbar = findViewById(R.id.toolbar);

and then set it by

setActionBar(toolbar);

After that it should be possible to use the getActionBar calls etc.

Hope it helps cheers ;)

Rafay Zia Mir
  • 2,116
  • 6
  • 23
  • 49
Neristance
  • 146
  • 3
  • Hey, How would I check/change the theme that I am using? Sorry I'm relatively new to android, thanks for answering though! – user3375184 Feb 12 '15 at 15:54
  • Hey if you didnt ad any Theme you Probably using the default one :) So check it in your AndroidManifest there should be an application inside an attribute: android:theme="@style/Theme.Holo" if not you should ad it and test your app again – Neristance Feb 12 '15 at 16:03
  • Must I add this theme to styles.xml as well? Otherwise it says 'Cannot resolve symbol "@style/Theme.Holo"' – user3375184 Feb 12 '15 at 16:09
1

So I ran the project myself. Theme.AppCompat only generates an ActionBar if your base activity is from ActionBarActivity. Use @android:style/Theme.Holo.Light.DarkActionBar instead of Theme.AppCompat in your styles.xml to generate an action bar by default.

The error is caused by attempting to call ActionBar methods when getActionBar() was returning null. The tutorial you linked does not show the styles.xml (and is also a very outdated tutorial!)

ekchang
  • 939
  • 5
  • 11
  • Seeing as you seem to know your stuff, I have a simple form that a user fills in and the data gets sent to an SQLite database, is it possible to have this form as a fragment under tab1 for example? No worries if you can't answer it, thanks. – user3375184 Feb 12 '15 at 17:52
  • You would need to add the form in the fragment tab 1 layout and/or in the FragmentTab1 class. Whatever you choose to display in that class/layout file will be what shows up in your tabbed display. – ekchang Feb 12 '15 at 18:25