MyoZaw Oo
MyoZaw Oo

Reputation: 127

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) produce NullPointerException

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.ParseAnalytics;
import com.parse.ParseUser;

import adapter.SectionPargerAdapter;


public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{

    public static final String TAG = MainActivity.class.getSimpleName();

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. 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}.
     */
    SectionPargerAdapter mSectionPargerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ParseAnalytics.trackAppOpened(getIntent());

        ParseUser currentUser = ParseUser.getCurrentUser();

        if(currentUser == null) {

            navigateToLogin();
        } else {
            Log.i(TAG, currentUser.getUsername());
        }

        final ActionBar actionBar = getActionBar();
//        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);



        mSectionPargerAdapter = new SectionPargerAdapter(this, getSupportFragmentManager());

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

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){

            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);

                for (int i = 0; i < mSectionPargerAdapter.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 callback (listener) for when
                    // this tab is selected.
                    actionBar.addTab(actionBar.newTab()
                            .setText( mSectionPargerAdapter.getPageTitle(i))
                            .setTabListener(MainActivity.this));}
            }
        });

    }

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

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

    }

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

    }

    private void navigateToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int itemId = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (itemId == R.id.action_logout) {
            ParseUser.logOut();
            navigateToLogin();
        }

        return super.onOptionsItemSelected(item);
    }
}

I face actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) has produce NullPointerException. How can I fix this error? I don't know how to fix. In tutorial video, this has no error but tutorial use with Eclipse. I use Android Studio. Please tell me how to fix? Thanks you very much!

Upvotes: 2

Views: 6442

Answers (2)

PiTTDRoiD
PiTTDRoiD

Reputation: 21

The real problem is in res/values/style.xml and res/values-v14/style.xml in the AppBaseTheme (in two styles).
If change Theme.AppCompat.Light and Theme.AppCompat.Light.DarkActionBar by Theme.Holo.Light and Theme.Holo.Light.DarkActionBar in manifest down android:minSdkVersion to version 11, it SOLVES the problem...
But it generates another problem: you lose AppCompat.Light theme...
But error NullExceptionPointer in actionbar.setNavigationmode is solved..

Upvotes: 2

Kostas Drak
Kostas Drak

Reputation: 3260

Take a look at this one over here. This guy implements it very nice. However i would not suggest working with actionBar tabs because if you want to upgrade your app to later versions this method is deprecated. So you dont use the actionBar and you can use the following if you would like

YourAdapter mAdapter;
    ViewPager mViewPager;

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

        // ViewPager and its adapters use support library
        // fragments, so use getSupportFragmentManager.
        mAdapter = new YourAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAdapter);
    }

The Adapter:

public class YourAdapter extends FragmentStatePagerAdapter {
    private String[] titles = { "Item 1", "Item 2", "Item 3" };
    public YourAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch(i){
           case 0:{
              return new FragementA();
           }case 1:{
              return new FragmentB();
           }case 2:{
              return new FragmentC();
           }
        }
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

The Fragment that you will return and implementation of the onCreateView Method:

public static class FragmentA extends Fragment {
    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.my_layout_file, container, false);
        //Simple implementation how to target text view in your layout
        TextView tv = (TextView)rootView.findViewById(R.id.my_text_view);
        return rootView;
    }
}

Upvotes: 3

Related Questions