0

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>
anu
  • 703
  • 1
  • 10
  • 19
  • In your Android manifest file under activity add `android:theme="@android:style/Theme.Holo"` or [use this solution](http://stackoverflow.com/a/27235537/2749470) – Bhargav Modi Mar 12 '15 at 05:53
  • Unable to start activity ComponentInfo{com.example.anusha.app/com.example.anusha.app.AddressList}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. – anu Mar 12 '15 at 06:03

1 Answers1

1

Change

ActionBar actionBar = getActionBar();

to

ActionBar actionBar = getSupportActionBar();


The reason to do this change is that: your activty is extending from ActionBarActivity, which means you are using ActionBar from support library, so you have to use getSupportActionBar().

Xcihnegn
  • 11,579
  • 10
  • 33
  • 33
Pooja
  • 2,417
  • 20
  • 39