Android actionbar not visible-ThrowExceptions

Exception or error:

I am trying to add an option menu in actionbar to my existing app but it is not working. If I create a new project with “hello world” default app I can see the button in action bar. The onCreateOptionMenu() method seems never caught in debug with breakpoint, what’s wrong ??

I’m working with API 14 on both app and this is my MainActivity code.

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

public class Principale extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ecran_d_acceuil);


}
public boolean onCreateOptionMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.principale, menu);
    return true;
}
 }

My menu.xml code

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
</menu>

manifest.xml

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myapp.Principale"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.myapp.Home"
            android:label="@string/title_activity_home" >
        </activity>
        <activity
            android:name="com.example.myapp.ListDir"
            android:label="@string/title_activity_list_dir" >
        </activity>
    </application>
</manifest>
How to solve:

Have you tried to set the app theme to a theme that supports the ActionBar, like Theme.Holo.Light?

Also, in your Activity you can try getActionBar().show();.

###

In my case, what fixed the issue, was the answer of this question; to remove

android:theme="@style/AppTheme"

from the tag in the Manifest.

###

Principale extends ActionBarActivity should take care of the issue

###

You have extended Activity. Try extending ActionBarActivity in java code

###

Spent 3 hours and eventually fixed it! Forget removing android:theme=”@style/AppTheme” from your manifest. It makes run-time error.
This is how I fixed the problem. Your activity should extend AppCompatActivity instead of Activity.

###

For your information, I solved my problem of the actionBar by going on the layout XML file and then pressing on View Option -> Show Layout Decorations.

View Option is the little eye just under the Select Design Surface

Hope it will solve your problem.

Cheers.

###

on create of your activity.

getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        customNav = LayoutInflater.from(this).inflate(
                R.layout.action_bar_customvieew, null);
        getSupportActionBar().setCustomView(customNav);

###

your main activity needs to be like this:

import android.os.Bundle;
import android.app.ActionBar;
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);

    ActionBar actionBar = getActionBar();
    actionBar.hide();

    actionBar.show();
    actionBar.setSubtitle("subtitle");
    actionBar.setTitle("title"); 


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_refresh:
      Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
          .show();
      break;

         default:
             break;
             }
    return true;
    } 



}

and your main.xml like this:

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

     <item
         android:id="@+id/action_refresh"
         android:orderInCategory="100"
         android:showAsAction="always"
         android:icon="@drawable/ic_action_search"
         android:title="Refresh"/>
    <item
        android:id="@+id/action_settings"
        android:title="Settings">
    </item>

</menu> 

this is a good and simple guide to creating an action bar:
http://www.vogella.com/tutorials/AndroidActionBar/article.html

###

Check Your manifest file In the Activity declaration if this line is there then delete it.

android:theme="@style/AppTheme.NoActionBar"

###

What solved my issue in this regard was, in the design view, click the eye icon (view options) and select Show Layout Decorations.

Image Showing the Eye Icon in the IDE of Android Studio 3.5

###

Make sure you don’t use:

requestWindowFeature(Window.FEATURE_NO_TITLE);

in your layout .xml file.

###

In my case
Action bar menu was not visible
when used
public class MainActivity extends Activity {
}
but was visible when used
public class MainActivity extends ActionBarActivity{
}

###

I had the same problem and I confirm that if you delete android:theme="@style/AppTheme" from AndroidManifest.xml, it will solve your problem.

###

you have to call the method setHasOptionsMenu(true); in the OnCreate Method

###

i am using Samsung young 2 as android emulator and i have the same problem ,i reslov this by removing
android:theme ="" from the manifest.xml and changing the theme to another theme support action bar

###

You should extend AppCompatActivity.

###

getSupportActionBar().show();

Simply paste this code in your java onCreate function.

Leave a Reply

Your email address will not be published. Required fields are marked *