-2

So I am just learning to write android apps, nothing specific yet but mainly toying around to see how they work. I tried to do something with tabs in the action bar, based off a tutorial I found, but I get a null pointer exception thrown before the app even launches.

Here is the code:

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

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

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

    for(String tab_names : tabs)
    {
        actionBar.addTab(actionBar.newTab().setText(tab_names).setTabListener(this));
    }

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
    {
        @Override
        public void onPageSelected(int position)
        {
            actionBar.setSelectedNavigationItem(position);
        }

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

        }

        @Override
        public void onPageScrollStateChanged(int arg0)
        {

        }
    });
}

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

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

    return rootView;
}

there are 2 other classes of the onCreateView with different layouts but are exactly the same otherwise. Here are the xml, the first is the main and the next for the fragment:

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



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ff8400">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/hello_world"
    android:textSize="20sp"
    android:layout_centerInParent="true"/>

I can't seem to figure out what is causing the thrown exception. I also can't seem to get logcat to export to a log file so I will try to get that on here as well.

Logcat

 02-28 01:36:42.431: I/Process(1629): Sending signal. PID: 1629 SIG: 9

   02-28 01:37:27.401: D/AndroidRuntime(1668): Shutting down VM

   02-28 01:37:27.401: W/dalvikvm(1668): threadid=1: thread exiting with uncaught exception (group=0xb4a1fb90)

   02-28 01:37:27.471: E/AndroidRuntime(1668): FATAL EXCEPTION: main

   02-28 01:37:27.471: E/AndroidRuntime(1668): Process: com.example.tolerablenausea, PID: 1668

   02-28 01:37:27.471: E/AndroidRuntime(1668): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tolerablenausea/com.example.tolerablenausea.MainActivity}: java.lang.NullPointerException

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.app.ActivityThread.access$700(ActivityThread.java:135)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.os.Handler.dispatchMessage(Handler.java:102)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.os.Looper.loop(Looper.java:137)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.app.ActivityThread.main(ActivityThread.java:4998)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at java.lang.reflect.Method.invokeNative(Native Method)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at java.lang.reflect.Method.invoke(Method.java:515)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
   02-28 01:37:27.471: E/AndroidRuntime(1668):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at dalvik.system.NativeStart.main(Native Method)

   02-28 01:37:27.471: E/AndroidRuntime(1668): Caused by: java.lang.NullPointerException

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.content.ContextWrapper.getResources(ContextWrapper.java:89)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.content.Context.getString(Context.java:343)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at com.example.tolerablenausea.MainActivity.<init>(MainActivity.java:29)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at java.lang.Class.newInstanceImpl(Native Method)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at java.lang.Class.newInstance(Class.java:1208)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)

   02-28 01:37:27.471: E/AndroidRuntime(1668):  ... 11 more
Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
user3363625
  • 241
  • 1
  • 2
  • 5

2 Answers2

1

Where the for each loop has been used, can't see the "tabs" being initialized. If it is null and there is no "String" in it.

Check:
If your android:minSdkVersion is set to 8 (API-8), it does not provide getActionBar() (only since API-11).

If lint warnings are suppressed, you won't get a compile time error for that.

Try http://actionbarsherlock.com/ - backward compatibility helps.

If you want to check, see if this works:

if (android.os.Build.VERSION.SDK_INT >= 11)
    getActionBar().setDisplayHomeAsUpEnabled(true);
Pararth
  • 8,114
  • 4
  • 34
  • 51
0

I think getActionbar() method return null value, you can follow the link provided below.

NullPointException with getActionBar,

getActionBar() returns null

It may help you. Mark as answer if it is helpful to you.

Community
  • 1
  • 1
Solution
  • 604
  • 4
  • 10