我尝试在我的地图活动中添加一个后退按钮,但它总是崩溃,这是我的操作栏代码
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapsanlocations);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
geocoder = new Geocoder(this, Locale.getDefault());
actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我还在android mainfest中加入了它的父母
<activity android:name=".Mapsanlocations" android:parentActivityName=".MainActivity" android:label="@string/title_activity_mapsanlocations"></activity>
提前致谢。
答案 0 :(得分:0)
使用getSupportActionBar()
代替getActionBar();
查看更多详细信息Given in this answer
答案 1 :(得分:0)
你可以在这里发布logcat以获得确切的答案......但是你可以
在onCreateOptionsMenu(菜单菜单)活动功能中执行以下操作。
MenuItem item=menu.add("Title"); //your desired title here
item.setIcon(R.drawable.icon); //your desired icon here
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
return false;
}
});