App keeps crashing. It says there is a null pointer exception at actionBar.setDisplayHomeAsUpEnabled(true);. I checked my code there is a button for the back button. It is defined and it goes back to the main maps activity. In onOptionsItemSelected(MenuItem menu) I have put the case for my back button. I don't know why it is giving me an error.
public class AddressList extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_address_list);
// get action bar
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
ListView listview = (ListView) findViewById(R.id.listView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_address_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (item.getItemId()) {
case R.id.button:
Add();
return true;
case R.id.back:
Back();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void Back(){
Button button = (Button) findViewById(R.id.back);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(AddressList.this, MapsActivity.class);
startActivity(intent);
finish();
}
});
}
private void Add() {
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(AddressList.this, SilentGeofence.class);
startActivity(intent);
finish();
}
});
}
}
Here is my menu.xml file You can see that I have a back button.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.anusha.app.AddressList">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@id/button"
android:title="+"
app:showAsAction="ifRoom">
<item android:id="@+id/back"
android:title="List"
app:showAsAction="ifRoom" />
</item>
</menu>