Java 尝试访问ActionBar时出现空指针异常
我目前正在尝试向我的操作栏添加自定义布局,但一直遇到空指针异常,无法找到解决方法 这是我的OnCreateOptions菜单Java 尝试访问ActionBar时出现空指针异常,java,android,Java,Android,我目前正在尝试向我的操作栏添加自定义布局,但一直遇到空指针异常,无法找到解决方法 这是我的OnCreateOptions菜单 @Override public boolean onCreateOptionsMenu(Menu menu) { ActionBar mActionBar = getActionBar(); LayoutInflater mInflater = LayoutInflater.from(this); View mCustomView =
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
ActionBar mActionBar = getActionBar();
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.menuText);
mTitleTextView.setText("Insert Title Here");
ImageButton imageButton = (ImageButton) mCustomView.findViewById(R.id.menuPlusButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Start New Course", Toast.LENGTH_LONG).show();
}
});
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
return super.onCreateOptionsMenu(menu);
}
错误似乎发生在mActionBar.setDisplayShowHomeEnabled(false)上,这表明ActionBar mActionBar=getActionBar()
返回null,我不知道为什么。我扩展了AppCompatActivity并运行min API 7
提前感谢。将
getActionBar()
替换为getSupportActionBar()
,并将导入内容更改为匹配。将getActionBar()
替换为getSupportActionBar()
并将导入内容更改为匹配。效果非常好。谢谢,很有魅力。谢谢