onCreateOptionsMenu compile error - java

I tried to use onCreateOptionsMenu in my application. I followed developers blog and it didn't work of me.
When i use this code:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.homepage_actionbar, menu);
return super.onCreateOptionsMenu(menu);
}
I got this compile errors:
Multiple markers at this line
- Syntax error on token ")", ; expected
- Illegal modifier for parameter onCreateOptionsMenu; only final is
permitted
- Syntax error on token "(", ; expected
Multiple markers at this line
- Void methods cannot return
a value
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/add_option"
android:title="Add Item"
android:icon="#drawable/ic_launcher"
android:showAsAction="ifRoom"
/>
</menu>
Thank for helping

I assume your onCreateOptionsMenu method to be implemented in another method of your Activity.
Just move it at the "root" level of your Activity class.

Check your code, that error is probably due to the fact a curly bracket is missing you either { or } or as #codeMagic said you have your code running in a method which is wrong you must have it directly in the class.

Related

How to add a menu folder to the res folder of an android studio project

I want to add a menu to my Android project and I created a menu folder inside the res folder, but got the error:
cannot resolve symbol 'menu'.
I was following an Android developers tutorial Menus | Android Developers
I will leave a screenshot of my project here:
project
And here is the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
game_menu.xml:
game_menu.xml
Try clean and after rebuild project, in menu Build on Android Studio.
Is that how you created it? you should try.
Res -> new -> Android Resource Directory -> Resource Type (Menu) -> Ok
Try to put it into one line of code like: getMenuInflater().inflate(R.menu.game_menu, menu);
First of all it is nicer coding I belive and that way we can be sure that the MenuInfalter is working. I have looked it up in my project and it is the same way there + working. If that does not help please share your game_menu.xml.
Use this code in your game_menu.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_settings"
android:orderInCategory="75"
android:title="my menu Item!"
app:showAsAction="never"/>

Chromecast button not visible in android

I am following the code lab tutorial.
My Gradle file looks like this
dependencies {
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:mediarouter-v7:25.0.0'}
This is my CastOptionsProvider class
public class CastOptionsProvider implements OptionsProvider {
#Override
public CastOptions getCastOptions(Context context) {
return new CastOptions.Builder()
.setReceiverApplicationId(context.getString(R.string.chromecast_app_id))
.build();
}
#Override
public List<SessionProvider> getAdditionalSessionProviders(Context context) {
return null;
}}
This is the menu xml file
<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">
<item
android:id="#+id/media_route_menu_item"
android:title="#string/media_route_menu_title"
app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
app:showAsAction="always" /></menu>
And this is my OnCreateOptionsMenu method in MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.navigation_drawer, menu);
mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu, R.id.media_route_menu_item);
return true;
}
And this in the manifest file
<meta-data
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="com.mypackage.CastOptionsProvider" />
I have followed the codelab tutorial to its exact form, copy and pasted everything while changing those variable which needs to be changed.
My application and the chromecast device are connected to the same network. The Chromecast button appears in the Youtube app but not on my app. What am I missing ?
Thanks
It might be an invalid app_id.
You might try replacing your chromecast_app_id string resource with the app_id string resource value from the sample project.
(Right now, it's 4F8B3483. See: https://github.com/googlecast/CastVideos-android/blob/master/res/values/strings.xml).
Believe it or not, switching to that value made the cast icon visible in my app.
If everything is done properly, you just need to restart cast device (it does not work with your app_id without restarting after registration).
Check in Google Cast SDK Developer Console if your Application ID is correct and status is Published.
When publish your Application ID, restart your Chromecast device.
For testing purposes, you can use CC1AD845 or 4F8B3483 as Application ID. These are from Google sample apps.
Adding another answer as the accepted answer did nothing for me. The issue happened to me only when building a proguarded version of my app.
I had to add a couple lines to my proguard file (essentially all of the classes referenced by xml as mentioned here: https://stackoverflow.com/a/24578823/1048847)
-keep class com.your.package.CastOptionsProvider { *; }
// I have a custom MediaRouteActionProvider but this may need to be
// android.support.v7.app.MediaRouteActionProvider in the case of OP
-keep class com.your.package.MediaRouteActionProvider { *; }
you are missing attribute icon here
<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">
<item
android:id="#+id/media_route_menu_item"
android:title="#string/media_route_menu_title"
android:icon="#drawable/chromebutton" app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
app:showAsAction="always" /></menu>

Stuck on WebView Tutorial - R.id issue?

Good morning - I hope everyone has had an enjoyable weekend.
I am having some issues following the tutorial at https://developer.chrome.com/multidevice/webview/gettingstarted
Everything is going well until I reach the step regarding an edit of the MainActivity class. This is Step 3 in the section Add the WebView: https://developer.chrome.com/multidevice/webview/gettingstarted#add_the_webview
Here is the content of my AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nathan.myapplication" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyActivity"
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>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
And here is my MyActivity.java:
package com.example.nathan.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
public class MyActivity extends Activity {
private WebView mWebView; // Added by ND Guthrie 8.15.2014:2229
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mWebView = (WebView)findViewById(R.id.activity_my_webview); // Added by ND Guthrie 8.15.2014:2231
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, 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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I have tried suggestions based on these links here on stackoverflow:
Error with R class import in android
Android Studio don't generate R.java for my import project
R.id cannot be resolved
For the error with R class import, I have attempted adding the line
import com.TestApp.HelloWebView.R;
But with no success, as TestApp is not recognized. This stands to reason, since I have not named anything 'TestApp' in my app here, but I do not understand how to fix it.
I also tried deleting the generated folder and cleaning and rebuilding the project. However, I obtain the same results.
I know this is probably a silly little thing, but I have been searching google and stackoverflow for days now, and it seems like there is just something I am not seeing.
Any ideas? Please advise.
Thank you very much for your time!
Best,
Nathan
The way it works is that your XML files get compiled into resources in the background by Eclipse, and an R.java gets generated that allows your project to refer to these resources. If you put "R.id.blah" or "R.layout.blah" or whatever into Eclipse and it isn't recognised, it means one of the following:
Your XML files don't have corresponding elements. R.java is being created, but doesn't contain "R.id.blah" or whatever you're using, because your XML files don't contain elements that would compile with those IDs.
R.java couldn't be compiled at all. That'll be the case if there are errors in your project. You're best off making sure that there are no errors (no red crosses anywhere), and then seeing whether R.java appears. Once you've done that, you'll be able to find out what IDs are being generated, and that should point you in the direction of what's missing or inconsistent in your XML files.

Reference to class file not working

I'm trying to make a simple RSS feed reader app. I've created a class file with the name "IotdHandler.java" and a public class with the same name. When I try and create a new instance of the class I get the following error: "Unreachable code".
If anyone can point me in the direction of a good tutorial about working with multiple class files I would be very grateful.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nasa_daily_image, menu);
return true;
IotdHandler handler = new IotdHandler();
handler.processFeed();
}
Thats because you have a return statement before.
Unreachable code has nothing to do with the class/library, is a compiling error because there's no way the code below the "return true" will be executed in anyway, you should get a good Java Book before diving into Android.
Regards!

Remove icon from launcher

It is most popular problem and no one know how to resolve it...
For example:
<application
android:allowBackup="true"
android:icon="#drawable/add"
android:theme="#style/CustomTheme"
android:showAsAction="ifRoom|withText">
And now i need to hide android:icon or just i wanna to remove it
<application
android:allowBackup="true"
android:theme="#style/CustomTheme"
android:showAsAction="ifRoom|withText">
But in this case i have default android icon -_-
It is possible to permanent remove this icon?
This can remove your launcher icon after next reboot:
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Also this link can help you too:
Hide application launcher icon in title bar when activity starts in android
You may create you own icon picture.
So you may try just create picture which has necessary size and consist from transparent background only.
I have a solution! I had began learn java and android a couple of day ago, so my solution need verify by someone more experienced then me. Here u have the solution:
Step1
In AndroidManifest.xml file application section we need to declare NoTitleBar theme.
<application
...
android:theme="#android:style/Theme.NoTitleBar">
...
</application>
Step2
Now we have no title bar when our application loader (We set Theme.NoTitleBar), so we need to create it or change android:theme. I take a second option, so:
public class MainActivity extends SherlockActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
....
}
....
}
R.style.AppTheme is a style created by us but we can use anyone available style from android API.
Step3
Now our title bar is back but still we have our icon and title. So we need turn off it and now we have two options for do this. First is when we use android api 11 or later and we not need use ActionBarSherlock libs and second is for application lower then api 11.
API 11 or lower
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
...
}
...
API 11 or later
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
...
}
...
So, in first case we use default getActionBar() to manage actionBar but in second case we use getSupportActionBar from ActionBarSherlock
Done :)

Categories