操作栏 activity 已关闭

Action Bar activity is closed

我的代码有什么问题?当我在 device/emulator 上 运行 应用程序时,我的应用程序崩溃了“不幸的是!操作栏已停止”

public class Main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ActionBar mActionBar = getActionBar();
    assert mActionBar != null;
    mActionBar.setDisplayShowHomeEnabled(false);
    mActionBar.setDisplayShowTitleEnabled(false);

    LayoutInflater mInflater = LayoutInflater.from(this);

    View myCustonView = mInflater.inflate(R.layout.actionbar, null);
    TextView mTitleTextView = (TextView) myCustonView.findViewById(R.id.title_text);
    mTitleTextView.setText("Some Text Here");

    ImageButton mImageButton = (ImageButton)myCustonView.findViewById(R.id.imageButton);
    mImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Refresh Clicked!" , Toast.LENGTH_LONG).show();

        }
    });

    mActionBar.setCustomView(myCustonView);
    mActionBar.setDisplayShowCustomEnabled(true);
}

}

注意: 我在 android 工作室工作,我已经从菜单文件夹中删除了 munu.xml 文件,并从 Main [=23] 中删除了它的代码=].

更改下一行

ActionBar mActionBar = getActionBar();

ActionBar mActionBar = getSupportActionBar();

希望对您有所帮助。

Nag 和 Maisse 已经为您提供了正确的答案,但如果这些答案不起作用,请试试这个。

查看 style.xml 以了解您的 activity 使用哪个主题。

<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

希望对您有所帮助。