getActionBar()抛出Null指针错误

时间:2016-09-04 18:53:30

标签: java android material-design

我在使用它时得到Null指针错误。

getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);

在我的扩展ListActivity的java类中 将我的主题升级为Theme.AppCompat后,我收到此错误。 我检查了stackoverflow上给出的所有解决方案,但没有任何帮助我。

MainActivity

  import android.support.v7.app.ActionBar;
     ......


     public class MainActivity extends AppCompatActivity
        {
          .....
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
           final ActionBar actionBar = getSupportActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            // Create the adapter that will return a fragment for each of the two
            // primary sections of the app.
            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

            // Set up the ViewPager with the sections adapter.
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);

            // When swiping between different sections, select the corresponding
            // tab. We can also use ActionBar.Tab#select() to do this if we have
            // a reference to the Tab.
            mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
            {
                @Override
                //if a tab was changed by a swipe gesture
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position); //update the tab bar to match the selected page
                    mDisplayedFragIndex=position;   //update the index of the currently displayed frag
                    if (position==1)  //if the view has moved to the history fragment:
                    {
                        mHistoryFrag.loadHistory(); //reload the history list view
                    }
                    invalidateOptionsMenu();
                }
            });

            ActionBar.TabListener tabListener=new ActionBar.TabListener(){
                @Override
                public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
                    mViewPager.setCurrentItem(tab.getPosition());
                }

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

                }

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

                }


            };


            // For each of the sections in the app, add a tab to the action bar.
            for (int i = 0; i < mSectionsPagerAdapter.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(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(tabListener));
            }//for
    .....
    }

这是xml

<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"
    tools:context=".MainActivity"
    />

我在这里使用viewpager我应该如何处理在activity_main.xml中声明工具栏xml和主活动中的代码时仍然出错。

Activity2 // 在此活动中我收到错误。 ....

public class Activity2 extends ListActivity
    {

    .....
    @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_chat);
            setupActionBar();
        }
         private void setupActionBar()
         {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
          {
           // enables the activity icon as a 'home' button. required if "android:targetSdkVersion" > 14

           getActionBar().setHomeButtonEnabled(true);
           getActionBar().setDisplayHomeAsUpEnabled(true);
          }
         }

    }

Activity2扩展了ListFragment

public class Activity2 extends ListFragment
{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_chat,
                container, false);
        setupActionBar();
        return view;
    }
    private void setupActionBar()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        {
            // enables the activity icon as a 'home' button. required if "android:targetSdkVersion" > 14
            getSupportActionBar().setHomeButtonEnabled(true); **//Getting Error in this line "Cannot Resolved "**
            //getActionBar().setHomeButtonEnabled(true);
            //getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
}//end of class

2 个答案:

答案 0 :(得分:1)

很明显。

MainActivity extends AppCompatActivity

AppCompatActivity延伸并且没有问题因为:

final ActionBar actionBar = getSupportActionBar();

getSupportActionBar()。但是,您在getActionBar()中使用Activity2ListActivity延伸至ListActivity并且this link表示;

  

AppCompat尚未移植到{{1}} 。可能 因为您应该将其视为“已弃用”,而是使用ListFragment

答案 1 :(得分:0)

你试过吗?

<强>更新

首先需要一个工具栏 -

Toolbar toolbar = findViewById(R.id.toolbar);// define in activity
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);