Java getActionBar()引发空指针错误
使用此命令时出现空指针错误Java getActionBar()引发空指针错误,java,android,material-design,Java,Android,Material Design,使用此命令时出现空指针错误 getActionBar().setHomeButtonEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true); 在扩展ListActivity的java类中 在将我的主题升级到theme.AppCompat之后,我遇到了这个错误。 我检查了stackoverflow上给出的所有解决方案,但没有任何帮助 主要活动 import android.support.v7.app.ActionBar;
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
.....
}
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
你试过了吗
更新
您首先需要一个工具栏-
Toolbar toolbar = findViewById(R.id.toolbar);// define in activity
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
很明显
MainActivity extends AppCompatActivity
从AppCompatActivity
扩展,这没有问题,因为:
final ActionBar actionBar = getSupportActionBar();
具有getSupportActionBar()
。但是,您正在Activity2
中使用getActionBar()
,该活动从ListActivity
扩展而来,如上所述
ListActivity
尚未移植到AppCompat
。可能是<强>,因为您应该考虑它“被弃用”,而使用ListFrace< /St>>/P>
是的,我试过它说“无法解析方法getSupportActionBar()”ListActivity不是AppCompative,这就是为什么不能使用getSupportActionBar()。这可能很有用:发布扩展类的完整代码。更新我的quesChange
Activity2
类以扩展ActionBarActivity。@jaydroider首先所有ActionBarActivity都被弃用,其次我不能使用“getListView()扩展后,我从ListActivity更新到listfragment,我仍然有很多错误,我仍然能够解决很多错误,但我在“getSupportActionBar()”中再次遇到错误,没有解决请使用新的活动代码和logcat更新您的问题。我编写了简单的代码(更新了我的问题),但在“getSupportActionBar()中仍然出现错误,即使我不能使用getActionBar()@MukeshGupta-我想你没有理解重点,你应该在主活动中获得工具栏(在本例中),请阅读此文档:-其中说明<代码>通过绑定到数据源(如数组或光标)来显示项目列表的片段,并在用户选择项目时公开事件处理程序。
MainActivity extends AppCompatActivity
final ActionBar actionBar = getSupportActionBar();