产生NullPointerException异常

产生NullPointerException异常

本文介绍了actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)产生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);
    }
}

我面对actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)具有产生NullPointerException异常。如何解决这个问题?我不知道如何解决。在视频教程,这没有任何错误,但教程中使用的Eclipse。我用Android的工作室。请告诉我如何解决?谢谢大家!

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!

推荐答案

看看这一个比的。这家伙实现它非常漂亮。但是我不建议,因为如果你想你的应用程序升级到更高版本的此方法去precated与动作条的标签的工作。所以你不使用的动作条,你可以使用下面的,如果你想

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);
    }

的适配器:

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];
    }
}

的片段,你将返回和实施 onCreateView 方法:

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;
    }
}

这篇关于actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)产生NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:15