java - getActionBar() 抛出空指针错误

标签 java android material-design

我在使用它时遇到空指针错误。

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

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

主要 Activity

  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 和主要 Activity 中的代码时仍然出现错误。

Activity2//在此 Activity 中出现错误。 ....

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

最佳答案

很明显。

MainActivity extends AppCompatActivity

AppCompatActivity 扩展并且没有问题,因为:

final ActionBar actionBar = getSupportActionBar();

getSupportActionBar()。但是,您在从 ListActivity 扩展的 Activity2 中使用 getActionBar() > 和 this link说;

ListActivity hasn't been ported to AppCompat. Probably because you should consider it 'deprecated', and instead use a ListFragment.

关于java - getActionBar() 抛出空指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39320404/

相关文章:

java - Volley AppController 类对象返回 null

java - 从另一个程序控制一个java程序

ios - 无法在类中导入 MaterialComponents

android - ViewPager 不显示工具栏和选项卡

Java 对象转换为整数和字符串包装器

java - 什么maven目标可以用于版本比较?

android - 杀死导航 Controller 中的 fragment

Android 深度链接 - 使用 Branch.io 链接未在 Android 中打开

android - 使用 configChanges 检查屏幕方向

android - 警报对话框按钮具有不同的颜色集