Neeraj Verma Neeraj Verma - 5 months ago 29
Java Question

Android Menu Not displaying

I tried to find solution and recreate Menu from this post (Contain Solution)

Menu items doesn't show up on the actionbar using Android Studio 1.4

but it displaying Menu like completely black Screen

this is my code

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:id="@+id/action_search"
android:icon="@drawable/common_full_open_on_phone"
app:showAsAction="always"
android:title="Search" />

<item android:id="@+id/action_compose"
android:icon="@drawable/common_full_open_on_phone"
app:showAsAction="ifRoom"
android:title="Compose" />

<item android:id="@+id/action_compose1"
android:icon="@drawable/common_full_open_on_phone"
app:showAsAction="ifRoom"
android:title="Compose" />

<item android:id="@+id/action_compose2"
android:icon="@drawable/common_full_open_on_phone"
app:showAsAction="ifRoom"
android:title="Compose" />

</menu>


Activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

</RelativeLayout>


MainActivity

package testproject.example.com.sensor_new;

import android.os.Bundle;
import android.app.Activity;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Toast.makeText(this, "onCreateOptionsMenu", Toast.LENGTH_LONG).show();
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}


}


Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="testproject.example.com.sensor_new">

<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<permission
android:name="testproject.example.googlemaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />

<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


I have even applied the style theme as given in the solution , But still I am not getting menu ,

Any Help would be appreciated

Answer

you have not initialized the toolbar.

  Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);

add the above code to your onCreate method