javaandroidmenuoncreateoptionsmenu

onCreateOptionsMenu Inflater doesn´t work in Android Java Class


in my code i want to infalte an action bar with a menu, but it doesn´t works. I have more Activities ( Start, Karte, Teilnehmer ) on all three I had place the onCreateOptionsMenu block but only on Karte it works. If anyone can help me, please anwser.

Start ( Menu not working )

package barsoftware.suedtirolpointer;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Start extends Activity {

Button button_karte;
Button button_teilnehmer;

////////////////////////////////// MENU //////////////////////////////////
@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_main, 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();

    if (id == R.id.menu_einstellungen) {

        return true;
    }
    if (id == R.id.menu_update) {

        return true;
    }

    return super.onOptionsItemSelected(item);
}

////////////////////////////// LAYOUT //////////////////////////////

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.start);


    button_karte = (Button) findViewById(R.id.button_karte);
    button_teilnehmer = (Button) findViewById(R.id.button_teilnehmer);

    // Capture button clicks
    button_karte.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            // Start NewActivity.class
            Intent karte = new Intent(Start.this,
                    Karte.class);
            startActivity(karte);
        }
    });

    button_teilnehmer.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            // Start NewActivity.class
            Intent teilnehmer = new Intent(Start.this,
                    Teilnehmer.class);
            startActivity(teilnehmer);
        }
    });
}


}

Solution

  • you need to extend AppCompatActivity which provide inbuilt Toolbar/ActionBar support so change

    Someclass extends Activity

    to

    Someclass extends AppCompatActivity