I can not make an Action Bar in my app. It crashes on startup. I have tested several code examples from the internet. I have made a completely new project in Eclipse with the only purpose of adding an action bar. But I can't.
Here is my code
package com.example.testmenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
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)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_save"
android:icon="#drawable/icon_one"
android:title="#string/menu_one"
android:showAsAction="ifRoom|withText" />
</menu>
java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.example.testmenu/com.example.testmenu.MainActivity}:
android.view.InflateException:
Binary XML file line #2:
Error inflating class menu
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
Without the menu-tag the app runs ok.
What am I doing wrong?
Your Menu should be from actionbarsherlock package. Try like this:
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
//...
or you can add import:
import com.actionbarsherlock.view.Menu;
Related
the button more information is not visible!!!!
xml of menu :-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/more_information"
android:title="#string/action_mnn"
/>
</menu>
main activity :-
package com.example.yashchaudhary.modernartui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.more_information:
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
screenshot after clickin on 3 dots
In my case it was a Theme specific for the action bar in the activity layout
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
.../>
Removing the theme property solved my problem
<androidx.appcompat.widget.Toolbar android:id="#+id/toolbar" android:theme="#style/ThemeOverlay.MaterialComponents.Dark.ActionBar"/>
Removing the theme attribute will resolve this problem.
Try to add to the item on the menu xml
android:orderInCategory="100"
and add break; after
case R.id.more_information:
return true;
I'm attempting to put a menu into a fragment in my app. However, the menu isn't appearing when I run it. My understanding of the steps involved in making a menu display in a fragment (and please correct me if I'm wrong or missing something) is that you do the following:
Create a menu resource file in the res/menu directory.
Override onCreateOptionsMenu(Menu, MenuInflater) and within said method, inflate the layout defined by the menu resource ID.
Notify the fragment manager that this fragment should receive a call to onCreateOptionsMenu by calling setHasOptionsMenu(true) in the fragment's onCreate method.
I've written a reduced version of my code to only include the bare minimum of what (I believe) should show a menu. Can anyone tell me what's missing from this code?
Here is my menu resource xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_1"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_item_text"
android:showAsAction="ifRoom|withText"/>
</menu>
My fragment code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_menu, menu);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, parent, false);
return v;
}
}
And my activity code:
package com.bignerdranch.android.fragmentmenuexample;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = new MainFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
And my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bignerdranch.android.fragmentmenuexample" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I'm running this on an AVD Galaxy Nexus running IceCreamSandwich.
Problem solved. My styles.xml file had the following:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
I added a new resource directory values-v14 and added the following style to it:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
I think you missed to override onCreateOptionsMenu in your MainActivity.
If you don't need to inflate any menu, simply return true :
#Override
public boolean onCreateOptionsMenu ( Menu menu ) {
return true;
}
U can simpley create a Android Sample Project, as guide you can choose with menu or othey style, you can find what different with your code.
You could override the following function in your activity.
#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;
}
This will solve the problem.
My ActionBar app is closing unexpectedly. Please help me with it ...
I want to build action bar for 2.3 version I have add all support libraries also ...but that giving unexpected closed message in android emulator.
main.xml code :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search / will display always -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_launcher"
android:title="#string/action_search"
android:showAsAction="ifRoom"/>
<!-- Location Found -->
<item android:id="#+id/action_location_found"
android:icon="#drawable/ic_launcher"
android:title="#string/action_location_found"
android:showAsAction="ifRoom" />
</menu>
and main_activity.java code:
package com.jinesh.m_learning;
import android.app.Activity ;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
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) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
I have
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class ActivityHome extends SherlockFragmentActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
...........
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
but the menu is not showing in a title bar, but in the bottom of a screen(by clicking "menu" button on device). Like by normal activity... what am i doing wrong??
menu xml is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_add_record"
android:icon="#drawable/ic_action_plus"
android:title="#string/add"
>
</item>
</menu>
please help! :)
you can get individual menu items to show in the ActionBar by adding the android:showAsAction="always" attribute
http://developer.android.com/guide/topics/resources/menu-resource.html
I am doing menu item. But i am not able to view the menu item in the screen. Can anybody tell me what is getting wrong or what other need to be added to get the code working. I have got totally stuck.
package com.example.androiddemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MenuItem extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.menu.menupage);
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menupage, menu);
return true;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/about"
android:icon="#drawable/images"
android:title="About"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/setting"
android:icon="#drawable/ic_launcher"
android:title="App Setting"
/>
</menu>
Here you have to set your activity's xml file.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Your activity's xml file.
}
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menupage, menu); // Here you are setting the menu whatever options you want.
return true;
}
}
After running this, just press on menu, It should show your options. If not post back.