0

I have never had this issue until now because I've just updated my project to API21 (from API20) and I get a NullPointerException on the lines #62 #63 #64 #65 #66 #67. Could you help me? This is my build.gradle:

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "active_news.activenews"
    minSdkVersion 10
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
     }
   }
 }

   dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:21.0.3'
      compile 'com.squareup.picasso:picasso:2.1.1'
      compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
      compile 'com.afollestad:material-dialogs:0.5.1'

MainActivity:

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener, android.app.ActionBar.TabListener {
CollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;
SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarIndeterminateVisibility(true);
    setProgressBarIndeterminateVisibility(false);
    setContentView(R.layout.activity_main);

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);

    if (!welcomeScreenShown) {

        MaterialDialogCompat.Builder dialogBuilder = new MaterialDialogCompat.Builder(this);
        dialogBuilder.setMessage(getString(R.string.Dialog_message));
        dialogBuilder.setTitle(getString(R.string.Dialog_title));
        dialogBuilder.setNegativeButton(getString(R.string.start), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialogBuilder.create().show();
        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putBoolean(welcomeScreenShownPref, true);
        editor.commit(); // Very important to save the preference
    }

    mCollectionPagerAdapter = new CollectionPagerAdapter(
            getSupportFragmentManager());

    final android.app.ActionBar actionBar = getActionBar();
    assert actionBar != null;
    actionBar.setDisplayShowTitleEnabled(true);   //line#62
    actionBar.setDisplayUseLogoEnabled(false);    //line#63
    actionBar.setHomeButtonEnabled(false);       //line#64
    actionBar.setIcon(R.drawable.ic_action_icon3);  //line#65
    actionBar.setTitle(R.string.app_name2);     //line#66
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);    //line#67

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setViewPager(mViewPager);
    tabs.setTextColor(getResources().getColor(android.R.color.white));
    tabs.setTextSize((int) getResources().getDimension(R.dimen.tab_text));

}

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

}

public void onTabSelected(ActionBar.Tab tab,
                          FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());

}

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

}

@Override
public void onTabSelected(android.app.ActionBar.Tab tab, android.app.FragmentTransaction ft) {
    mViewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(android.app.ActionBar.Tab tab, android.app.FragmentTransaction ft) {

}

@Override
public void onTabReselected(android.app.ActionBar.Tab tab, android.app.FragmentTransaction ft) {

}

public class CollectionPagerAdapter extends FragmentPagerAdapter {

    //final int NUM_ITEMS = 11; // number of tabs

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

    }

    @Override
    public Fragment getItem(int position)
    {

        switch (position) {
            case 0:
                return new TAB1();
            case 1:
                return new TAB2();
            case 2:
                return new TAB3();
            case 3:
                return new TAB4();
            case 4:
                return new TAB5();
            case 5:
                return new TAB6();
            case 6:
                return new TAB7();
            case 7:
                return new TAB8();

        }

        return null;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position)
        {
            case 0:
                return getString(R.string.label1);
            case 1:
                return getString(R.string.label2);
            case 2:
                return getString(R.string.label3);
            case 3:
                return getString(R.string.label4);
            case 4:
                return getString(R.string.label5);
            case 5:
                return getString(R.string.label6);
            case 6:
                return getString(R.string.label8);
            case 7:
                return getString(R.string.label10);
            //case 8:
              //  return getString(R.string.label9);
            //case 9:
              //  return getString(R.string.label7);
        }
        return null;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    int i = item.getItemId();
    if (i == R.id.actiorn) {
        Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
        MainActivity.this.startActivity(intent);
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }
}

that's my logcat(where it displays the NullPointerException in the line #62):

12-30 01:00:24.249  25790-25790/active_news.activenews E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{active_news.activenews/active_news.activenews.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
    at android.app.ActivityThread.access$600(ActivityThread.java:150)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5191)
    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:795)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at active_news.activenews.MainActivity.onCreate(MainActivity.java:62)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
    at android.app.ActivityThread.access$600(ActivityThread.java:150)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5191)
    at java.lang.reflect.Method.invokeNative(Native Method)
Rick
  • 3,943
  • 6
  • 33
  • 45

1 Answers1

0

You are using AppCompat library, so I imagine you would have to call getSupportActionBar() instead of getActionBar()

EDIT: As I said before, if you updated your min SDK to 11, then you DO NOT NEED to use the getSupportActionBar. If there are still errors in the getActionBar methods, then make sure you did not use the theme android:theme="@style/Theme.NoTitle" on your Application manifest. You need to have a actionbar with title defined in your theme like android:theme="@android:style/Theme.WithActionBar". Or use getWindow().requestFeature(Window.FEATURE_ACTION_BAR); after setContentView. There are many solutions: getActionBar returns null

Community
  • 1
  • 1
mthandr
  • 3,062
  • 2
  • 23
  • 33
  • what is your min SDK? – mthandr Dec 30 '14 at 01:52
  • those actionBar methods have min SDK 11. I don't know how even the compiler is letting you do that. – mthandr Dec 30 '14 at 01:55
  • Your activity should extend the ActionBarActivity and use the getSupportActionBar() if you want to use SDK 10. – mthandr Dec 30 '14 at 01:55
  • _public class MainActivity extends FragmentActivity implements ActionBar.TabListener, android.app.ActionBar.TabListener_ – Rick Dec 30 '14 at 01:56
  • did you fix your problem by increasing the SDK to 11? FragmentActivity does not allow getSupportActionBar – mthandr Dec 30 '14 at 02:25
  • I did not resolve my problem. I updated the SDK to 11 but I keep getting red text on _getSupportActionBar_ – Rick Dec 30 '14 at 02:29
  • You do not need it anymore since you are using sdk 11. those methods that had nullpointer should work well now – mthandr Dec 30 '14 at 02:31
  • I'm getting _NullPointerException_ again – Rick Dec 30 '14 at 02:35
  • _actionBar.setDisplayShowTitleEnabled(true);_ and the others under it – Rick Dec 30 '14 at 02:36
  • can you post the code of the method you are calling those methods from? – mthandr Dec 30 '14 at 02:37
  • I extended to _ActionBarActivity_ and put _ActionBar actionBar = getSupportActionBar();_ instead of _final android.app.ActionBar actionBar = getActionBar();_ but I don't get customized ActionBar like the one I had with API20 – Rick Dec 30 '14 at 17:38
  • As I said before, if you updated your min SDK to 11, then you DO NOT NEED to use the getSupportActionBar. If there are still errors in the getActionBar methods, then make sure you did not use the theme android:theme="@style/Theme.NoTitle" on your Application manifest. You need to have a actionbar with title defined in your theme like android:theme="@android:style/Theme.WithActionBar". Or use getWindow().requestFeature(Window.FEATURE_ACTION_BAR); after setContentView. There are many solutions: http://stackoverflow.com/questions/10031180/getactionbar-returns-null – mthandr Dec 30 '14 at 17:43