Android - HelloWorld app in eclipse - java

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

Related

Cannot resolve symbol 'menu' errors in Android Studio

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

Why i'm getting errors when creating a new eclipse android project?

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.

My eclipse can´t resolved My_menu.xml from res/menu

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

3 dot setting menu for android apps with custom title

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();
}
}

activity_main cannot be resolved or not a field

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.

Categories

Resources