maybe I have stupid question, but I don´t know where is problem. So I have two xlm files in res/menu folder. First call "notes_edit_menu", and second "activity_draw". But If I want use it in code, like this.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.notes_edit_menu, menu);
return true;
}
I can use only "activity_draw". Because "notes_edit_menu", can not be resolved. Other files form res, for example layouts work fine.
Okay this can be two things:
1: You imported the wrong R class
Check your imports that you correctly imported your own "package.package.R" and not the android.R.
2: Do a clean build this rebuilds R
Related
I have been in the process of developing a mobile game using android studio. I am trying to create an options menu off of the main menu screen but anytime the app is ran and the options menu is accessed, the app crashes before loading the options menu. I have laboured over the code I have written and attempted to find what the problem is however I am unable to discover why the code is crashing.
If needed I can provide the java class I have been working with. It would be a great help if anyone could tell me what or why the code may be crashing.
Need more context, code or log would be helpful.
You will need to override these 2 methods in your Activity. first is to create menu, the second one is to handle the option select.
#Override
public boolean onCreateOptionsMenu(Menu menu) {}
#Override
public boolean onOptionsItemSelected(MenuItem item) {}
Check: https://developer.android.com/guide/topics/ui/menus#options-menu
I'm basically writing my first hello world in android studio and the java file says that the xml layout file and other resources in the res file don't exist. I took the references to these things from books/tutorials so they should work.
The book said to use
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main.xml);
}
But when that didn't work I changed it to res.layout.activity_main.xml
my project directory looks like this
How do I make it work?
http://i.stack.imgur.com/N71sl.png
//EDIT
http://i.stack.imgur.com/HUgsm.png
You are referencing res not R.
do something like this: setContentView( R.layout.activity_main);
Leave off the ".xml"
update your android studio in stable channel and restart android studio and build the project again .
You need to understand the concept of the R class in android.
The R class is auto generated every time you build your project and cannot be modified.
This class contains a map of all the projects assets. Every time you created or import a resource, set a dimension, a string, create a layout, add or change id etc. the R file is changed in the background.
So if you want to access a resource you simply call it using the R class.
For example:
layout: R.layout.activity_main
string: R.string.hello_world
id: R.id.et_main_helloWorld
And so on.
More info can be found here.
Make sure you also check Providing Resources and Accessing Resources for a better understanding.
Good luck and happy coding.
In the end I started a new project and added in everything piece by piece, I had put a mp3 file in the values directory which was messing up the R file.
well i am completely new in making Android application.I m not able to remove errors in the source code . here it is , source code -
"its not able to resolve R as a variable "
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
If the R file is missing, here are several solutions:
Select the project, click Project - Clean, clean up projects;
Select the project, right-select Android Tools - Fix Project Properties;
Select the project, right-select Properties - Android, select the appropriate version, and click OK;
Thus R files will be re-generated.
Look at your imports. check if this line exists:
import android.R;
If present, remove it, so that your project will resolve R not with the default Android Resources class, and import R with your package name
Try Clean & build. There is no problem with your java file.
If it still shows the error, Check out layout/activity_main.xml . Did you write something there which is not syntactically correct?
3.If you didn't change anything there, I suggest you to restart eclipse & clean-build again. Make sure yuor anot importing android's R file , you will need to import your project's R file.
There should not be any other cause.
Check your AndroidManifest.xml file it may contains some erroer by which your R.java not genrated.
or
Check with new eclipes
and in Your activity.java file Press CTRL + SHIFT + O
Its your IDE problem, eclipse right... ? sometime eclipse can't generate R.java,
Im suggest you to take course from here
https://www.udacity.com/course/ud853
and if you have using Eclipse as IDE, try to change into AndroidStudio, if you have facing some problem while using AndroidStudio join Community on Gplus here https://plus.google.com/communities/114791428968349268860
Cheers
in an android app,
if you create a new project,
then automatically the 3 dot settings menu is created on phones where it is needed
and it is handled the same way as it would have in older versions by:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
if you create a custom title, you have to add the 3 dot menu yourself
how do I know when it is needed? meaning phones that don't have the settings button
also how do i create a context menu that is customized and attached to the 3 dot button
Edit:
after a disscusion with Shobhit Puri, I understood that I was not considering the actionbar, since I am using a minimum API 8, I don't have it,
so there is the option that CommonsWare just supplied to check if the settings menu exists (I still need to check if it exists in API 8)
Shobhit Puri's suggestion was:
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(...) ;
ActionBar ab = getActionBar();
ab.setTitle("My Title");
ab.setSubtitle("sub-title") ;
but that of cores requires API 11 or the support library V7
either way I am excepting Shobhit Puri's answer, because of all his help, and I will post my final solution when I know it works
also thanks to CommonsWare for a nice answer
Edit2:
I decided to go with CommonsWare solution for now, I wrote it like this:
if (android.os.Build.VERSION.SDK_INT >= 14) {
ViewConfiguration vc = ViewConfiguration.get(this);
if (!vc.hasPermanentMenuKey()) {
setting_dots.setVisibility(View.VISIBLE);
setting_dots.setOnClickListener(this);
registerForContextMenu(setting_dots);
}
}
ideally I think you should use the actionbar, because it provides you with most of the work, but it has a lot of compatability issues with API 8 which for now I would rather avoid
As #dumazy pointed out that the Action bar's Menu Overflow icon is only shown on those devices which do not have a hardware menu-button.
How do I know when it is needed? meaning phones that don't have the settings button
This is handled by Android itself. You don't need to worry.
how do i create a context menu that is customized and attached to the 3 dot button
You can just have a an xml file inside Menu folder in res. Then you can specify the xml file inside the MenuInflater. Eg:
lets name it list_menu.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:title="#string/menu_string_1"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_item_1"
android:title="#string/menu_string_2"
android:showAsAction="ifRoom|withText"/>
</menu>
In the onCreateOptionsMenu you can set it as:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
}
This menu would be attached to the overflow-icon and have the items that you want to show when it is clicked. There are some hacks like this which can show the overflow-icon on all devices but using them is highly discouraged. Let android handle this itself.
You seem to use Title bar. Instead, try to use Action Bar for the same.
Hope this answers your question.
how do I know when it is needed? meaning phones that don't have the settings button
Call hasPermanentMenuKey() on a ViewConfiguration.
also how do i create a context menu that is customized and attached to the 3 dot button
By programming. Since you are not using an action bar, it is impossible to give you specific advice that would be relevant.
Google says Actionbar overflow only appears on phones that have no menu hardware keys. Phones with menu keys display the action overflow when the user presses the key.
If you still want to implement this you may follow this solution.
Click here for your reference.
Just copy this method in your activity and call the method from onCreate method
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
I am a beginner in android and I was trying to make a simple application on android and this "activity_main" error is not getting cleared.I have gone through all the available answers
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
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) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu); return true;
}
}
Clean project then try to run it. I faced the same problem. You can also do this:
Remove from your code following imports import android.R; or import your.application.packagename.R;
Now clean the project and run it.
Let me explain(put more detail) what exactly is happening with the above code.
FYI, there are two types of R.java file exists:
one is your project's R.java file, which you would use to access resources/layout of your project.
second is Android's R.java file which contains ID/index of the native resources like anim, color and many more resources, which you can access by using android.R.color.black way.
Mistake:
Now, you are making mistake by importing android.R which is not allowing you to access main_activity.xml of your project.
Solution:
Remove android.R and import R file of your project. You can press CTRL + SHIFT + O simply, will display available options.
U seem to have imported wrong R.java class
problem is in the first line "import android.R"
use your applications R.java class
Make sure you have the activity_main.xml file spelled correctly. I experienced the same problem due to having a misspelled file name.