0

I seem to have broken something in my Android app but I can't figure out what it is... I'm pretty new at this, it's probably something simple.

10-26 00:12:12.182    2928-2928/com.myapp.application E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.application/com.myapp.application.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.myapp.application.MainActivity.onTabSelected(MainActivity.java:94)
            at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
            at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:499)
            at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:485)
            at com.myapp.application.MainActivity.onCreate(MainActivity.java:45)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

Code: http://pastebin.com/9R5WM5M9

2
  • No one can help you without seeing the code. But if you look at the stacktrace, then you see that the problem is in line 94 in the MainActivity class.
    – SimonSays
    Oct 26, 2013 at 0:29
  • That's an auto-generated line. pastebin.com/9R5WM5M9
    – user1481539
    Oct 26, 2013 at 1:04

2 Answers 2

0

It appears that the null pointer is inside the onTabSelected event handler, most likely because mViewPager is null upon the first onTabSelected event. Try this:

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    int tabPosition = tab.GetPosition();

    if (null != mViewPager)
    {
        String newTitle;
        mViewPager.setCurrentItem(tabPosition);

        switch (tabPosition)
        {
            case 0:
                newTitle = getString(R.string.action_title_timeline);
                break;
            case 1:
                newTitle = getString(R.string.action_title_mentions);
                break;
            case 2:
                newTitle = getString(R.string.action_title_inbox);
                break;
            case 3:
                newTitle = getString(R.string.action_title_search);
                break;
            case 4:
                newTitle = getString(R.string.action_title_profile);
                break;
        }

        if  (null != newTitle && !newTitle.isEmpty())
        {
            setTitle(newTitle);
        }
    }
}
2
  • That got it, though I had to wrap R.string.action_title_* in getString()... Any reason those are being cast as integers? (Perhaps a lifetime with loosely-typed languages has ruined me here)
    – user1481539
    Oct 26, 2013 at 1:44
  • great! I forgot about the getString. I believe the reason why it was returning an int was because w/out getString, it was simply just returning the resource ID instead of the string itself.
    – d.moncada
    Oct 26, 2013 at 16:21
0

Yea, it's probably the ViewPager that is null. Try to get the reference before you add the tabs.

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

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

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    actionBar.addTab(actionBar.newTab().setIcon(R.drawable.ic_action_inbox).setTabListener(this));
   ...

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.