0

I tried to add a back button in my Map activity but it always crashes that is my code for the action bar

    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);
    }
}

and I also included its parent in the android mainfest

    <activity
        android:name=".Mapsanlocations"
        android:parentActivityName=".MainActivity"
        android:label="@string/title_activity_mapsanlocations"></activity>

Thanks in advance.

Khalid Bakry
  • 59
  • 1
  • 9

2 Answers2

0

Use getSupportActionBar() instead of getActionBar();
check out more detail Given in this answer

Community
  • 1
  • 1
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
0

Could you post the logcat here for exact answer...but yeah you could

Do as follows in onCreateOptionsMenu(Menu menu) function of activity.

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;
    }
});
Aakash
  • 151
  • 9