activity_main cannot be resolved or not a field - java

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.

Related

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.

Android - HelloWorld app in 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

Eclipse error: cannot be resolved to a type

With a friend's help, I made a simple app for school that loads a webpage like an iframe into it. Next, I need to learn how to add a share button so it's users can tell their friends about my app. I found this code and tried to add it to my files in eclipse, but it gives me some errors. I have searched a lot for the solution and I think there are several possibilities to solve it, such as refreshing the project, however I'm so new I'm just worried that I'll mess up my project somehow, so I feel it's best to ask first :)
This is the code I added:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_share_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
ShareActionProvider myShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_SEND);
myIntent.putExtra(Intent.EXTRA_TEXT, "Whatever message you want to share");
myIntent.setType("text/plain");
myShareActionProvider.setShareIntent(myIntent);
return true;
}
And the errors are:
Menu cannot be resolved to a type
MenuItem cannot be resolved to a type
ShareActionProvider cannot be resolved to a type
ShareActionProvider cannot be resolved to a type (yes the same error twice)
Hope someone can help. Thanks all.
are your import statements proper? try:
project Clean and Rebuild
android sdk selected correctly
Have you imported them?
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
Also note that ShareActionProvider is only available in Ice Cream (API 14) or newer. If you are targeting a lower API level you have to use
import android.support.v7.widget.ShareActionProvider;
and add the support library to your project.

Manually registering a new activity in manifest.xml

tl;dr Look at title + netbeans
I've been writing a game as I have been learning both Java and Android*. Most of it has gone without a hitch- but here we go.
To my understanding- each screen (main menu, gameplay, highscores, ect) is a separate Activity and therefore needs a different layout.
Problem 1:
EDIT: Solved. Thanks Daniel.
Problem 2: Then in the GameActivity.java file there is an error of cannot find symbol (symbol main_1 location class layout). Help?
package lolfighter.notriot;
import android.app.Activity;
import android.os.Bundle;
import lolfighter.notriot.nojoke.R.*;
// #author DEVELOPMENT
public class GameActivity extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(layout.main_1);
}
Also why is the bundle icicle? I have not specified this anywhere.
Edit- Not solved
*- I am in a partnership of sorts with a friend- the original plan was that he would handle all the code and I would handle all the graphic resources (He took an actual class for the Java language) But he has ran into a roadblock. I can't ask him because he has limited internet access (lunch breaks only) and lives in a different state (we go to college together)
Problem 2: then in the GameActivity.java file there is an error of cannot find symbol (symbol main_1 location class layout). Help?
Change layout.main_1 to R.layout.main_1.
Also why is the bundle icicle? I have not specified this anywhere.
You can rename the variable to be whatever you want, it doesn't matter.

"R cannot be resolved to a variable" just after creating android project? [duplicate]

This question already has answers here:
"R cannot be resolved to a variable"? [duplicate]
(30 answers)
Closed 9 years ago.
Just after creating the new Android Application Project I get the error "R cannot be resolved to a variable" in my MainActivity.java file.
This is it's content:
package com.example.firstapp;
import android.os.Bundle;
import android.app.Activity;
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.activity_main, menu);
return true;
}
}
Why am I getting this problem if the app was created by Eclipse choosing all the default options? How can I fix this issue?
It might be because, there might be some problem in your res folder,
The Main reason might be there could be wrong import file of R.java. So Remove, if any import android.R;
There might be some images with same name, or some files with capital letters, or even there might be some errors in your xml files,
Try rebuilding and cleaning your project.
For some quick solutions to this problem, have a look at this,
R.java file not getting created
first try to clean your project and after that build it through eclipse.If the problem persists after this operation than it means its because of your activity_main.xml

Categories

Resources