我想創建android的自定義動作條, 這是我簡單的代碼: 公共類MainActivity擴展活動{如何在android中創建自定義動作條?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menubar();
}
public void menubar(){
ActionBar mActionBar = getActionBar();
LayoutInflater inflater = getLayoutInflater();
View mCustomView = inflater.inflate(R.layout.menu_bar, null);
ImageButton button = (ImageButton) mCustomView.findViewById(R.id.bt_menu);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Clicked!",Toast.LENGTH_LONG).show();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
}
,但是當我運行顯示的錯誤是這樣的:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setCustomView(android.view.View)' on a null object reference
at dot.com.coba.MainActivity.menubar(MainActivity.java:39)
at dot.com.coba.MainActivity.onCreate(MainActivity.java:21)
我嘗試那樣,但不能工作, 無法解析方法'getsupportActionBar' – Bekti