I'm learning Android Development for Beginners from Udacity and have completed making an app named Court Counter in which I wrote code for an XML (activity_main.xml) and some code for Java (MainActivity.java) file as some of the code was provided by the Udacity team on Github.
But when I click on 'Build APK' I get two errors both from the Java file:
Cannot resolve symbol 'menu' in the following code:-
#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;
}
Cannot resolve symbol 'action_settings':-
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
I don't know what happens when these block of code are executed but I want to run the app on my phone!
With more than 20 years of experiencie i can tell you a very difficult solution for the menu problem on android studio...:
*********** Close the project an open it again ************************
I would have love to add this as a comment to the first answer but I can't for now; So when I had the problem using android studio 2.3.
I clean the project and then rebuild:
Build --> Clean Project and
Build --> Rebuild Project
Check menu_main.xml is in your res directory. After its not working means, Once, Clear Your project.
Try this
1) "alt+enter" (R.menu.menu_main, menu)
2) click on make a menu main.xml file (a new file)
3) Disable your antivirus
4) Run the app
Sometimes the error may arise because Android Studio failed to import R symbol.
import com.example.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
In Eclipse i'm going to File > New > Other > Android Application Project
I give a name then click next next next finish when it's creating the new project i'm getting errors in the console window:
error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'
6 errors all in styles.xml
Also i'm getting errors in the MainActivity.java and i didn't change anything didn'nt even open the MainActivity.java yet.
This is the main activity java code i didn't touch or changed anything.
package com.example.texttospeech;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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);
}
}
All packages in the android sdk manager already installed.
i'm targeting the project to compile on minimum 7 maximum 18.
First it was 21 so i created new project targeting and compiling 18.
But same errors. I didn't have it before.
I remember i deleted a project name: appcompat_v7 maybe i need this ? If so how to get it ?
Anyway i googled and so far no solution for me.
Go to the android sdk folder-> extras -> android -> support -> v7
Import the appcompat folder present in v7 folder in eclipse and then add this library to your project. This should solve your problem.
Do the following steps , this may help:
import android.app.Activity; paste this line in imports
Replace ActionBarActivity with Activity.
Remove all methods except onCreate for now.
Clean and Rebuilt project.
if problem still persist , restart eclipse.
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
I have been following the instructions for the Android Bootcamp youtube series (3. Hello World) as closely as possible, but I have run into a whole slough of issues:
Problem 1: AVD Manager
Eclipse won't recognize my phone (Samsung Galaxy S5) for debugging/running apps. It doesn't show up in the AVD at all.
I have downloaded the driver required for my comp to communicate with the phone
I have enabled Developer's Mode on my phone
I have enabled debugging over USB
I have restarted Eclipse AND my phone
The phone is new, undamaged and runs just fine
My computer recognizes my phone and attributes the correct name, can extract files, etc
Problem 2: Running Apps
I attempted to circumvent the AVD Manager by clicking "Run" to simply select my phone. However, after electing to run as an "Android Application," I got an error:
Errors occurred during the build.
Errors running builder 'Android Pre Compiler' on project 'XXX'
I resolved this error by installing new software from (Help > Install new Software > http://download.eclipse.org/releases/luna > 'Collaboration' > 'Subversive SVN JDT Ignore Extensions' and 'Subversive SVN Team Provider') via the recommendation made on this thread. This made the error disappear.
Unfortunately, while the error no longer appears, the application still refuses to run. There are errors now in:
HelloWorld1 (ERRORS)
src (ERRORS)
com.example.helloworld1 (ERRORS)
HelloWorldActivity.java (ERRORS)
and
rec (ERRORS)
values (ERRORS)
values-v11 (ERRORS)
values-vw820dp (ERRORS)
Sadly, I don't know when the errors appeared, but I do know that they are preventing me from running anything.
I have attempted cleaning the project, but it fails every time and does not return a warning or message.
The only file I have touched since creating the project is the xml fragment. I click-and-dragged a couple elements in the Graphical Layout, then removed them. I have not touched any code, except the hex color code used to change the font color for "Hellow World" in the HelloWorldActivity. the hex color code is correct and works just fine.
Code for XML frag:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.helloworld1.HelloWorldActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp"
android:text="#string/hello_world" />
</RelativeLayout>
Code for HelloWorldActivity.java:
package com.example.helloworld1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class HelloWorldActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello_world, 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 been dealing with nothing but errors for 4 days. I haven't gotten to do any actual work yet. I'm at wit's end.
Any and all help is deeply, deeply appreciated.
looks like a problem from your configuration
compare Project->Properties between your buggy project and the new one you created
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();
}
}