【问题标题】:getActionBar() throwing Null pointer errorgetActionBar() 抛出空指针错误
【发布时间】:2016-09-04 18:53:29
【问题描述】:

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

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

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

主活动

  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

【问题讨论】:

  • 发布完整的 code 与 extends class。
  • 更新了我的问题
  • 将Activity2 类更改为extends ActionBarActivity。
  • @jaydroider firstofall ActionBarActivity 已被弃用,第二件事我在扩展此功能后无法使用“getListView()”

标签: java android material-design


【解决方案1】:

很明显。

MainActivity extends AppCompatActivity

从AppCompatActivity 扩展,这没有问题,因为:

final ActionBar actionBar = getSupportActionBar();

有getSupportActionBar()。但是,您在从ListActivity 延伸的Activity2 中使用getActionBar(),正如this link 所说;

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

【讨论】:

  • 所以我从 listactiviy 更新到 listfragment 我有很多错误我仍然能够解决很多但我再次在“getSupportActionBar()”中遇到错误没有解决
  • 请使用新的活动代码和 logcat 更新您的问题。
  • 我已经编写了简单的代码(更新了我的问题)在“getSupportActionBar”中仍然出现错误,即使我无法使用 getActionBar()
  • @MukeshGupta - 我认为你没有明白这一点,你应该得到Toolbar(在这种情况下)主要是Activity,请阅读这个文档:developer.android.com/reference/android/app/ListFragment.html - 其中说; A fragment that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item.
【解决方案2】:

你试过了吗?

更新

你首先需要一个工具栏-

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

【讨论】:

  • 是的,我试过它说“无法解析方法 getSupportActionBar()”
  • ListActivity 不是 AppCompatActivity,这就是你不能使用 getSupportActionBar() 的原因。这可能有用:stackoverflow.com/questions/20524008/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 2020-01-24
  • 2020-11-22
相关资源
最近更新 更多