R cannot be resolved to a variable, rebuilding doesn't solve - android

Can someone help me?
public class Maths4to5Home extends Activity implements OnClickListener
{
private Button button_4to5PlayGame, button_mathsInstructions, button_4to5Scores;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maths4to5home); //error
button_4to5PlayGame = (Button)findViewById(R.id.button_4to5PlayGame); //error
button_mathsInstructions = (Button)findViewById(R.id.button_mathsInstructions); //error
button_4to5Scores = (Button)findViewById(R.id.button_4to5Scores); //error
I have included a sample of code above and highlighted where my errors are. I am being constantly told that R cannot be resolved to a variable. I have tried cleaning and rebuilding. Also I have tried importing R (as the quick fix suggests) but then I am told that for example in my setContentView() that
activity_maths4to5home cannot be resolved or is not a field
Any help would be greatly appreciated!

You need to import the correct R — specifically, the one for your project. This is based on what's in your project's manifest. If your manifest declares the project's package to be com.example.my_project then you need to import com.example.my_project.R;.
If your activity is already in the project's package, then you do not need to import R. However, you specifically need to not import android.R.
The error message may also be caused by a missing R.java. This can happen if errors in your resources prevents R.java from being generated. You should check that there are no errors in any of your resource files.

Related

setContentView(R.layout.main); - cannot find package

I have never used Android Studio before and have always been coding using NetBeans.
I need to create an app for Android, so I decided to use NetBeans. I created new project and it is 'empty' for now, however I have an error:
package Fridge.Freezer;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
The error is in setContentView(R.layout.main); - cannot find package. Can anyone tell what I am doing wrong? Clean and build does not work.
erase your package name at the top and try to add it to getting the suggestion package import by shortcut Control + Enter beside "R"
Try checking following component in your project directory
R.java file
folder named layout
layout xml file named main.xmlmain
PS Since your are starting a fresh project, I would suggest you to start at Android Studio.
I tried cleaning the project and rebuilding it again. It did not work. The solution was to restart IDE.

Cannot get reference to layouts and id in res. folder

This question has been up a couple of times but still I haven't found an answer that helped me.
1:st one:
I cannot find any of my R.Layout.activity_article_detail and other layout xml files through reference. It stopped working after did a "clean project" in Eclipse and tried to build it up again.
I've tried removing and adding and removing the .R import but right now it wont find any of them. My layout files doesn't include a Capital letter, are spelled correcly.
code:
package martin.larsson.kopingsrssreader;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import martin.larsson.kopingsrssreader.R;
public class ArticleDetailActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article_detail);
Second problem is it cant find my res.menu either. I got 2 files in it. detailmenu.xml and refreshmenu.xml. In both i cant find any of the id tags
<item android:id="#+id/actionbar_markunread"
<item android:id="#+id/actionbar_saveoffline"
<item android:id="#+id/actionbar_refresh"
Does anyone got a clue?
SOLUTION: Uncheck "build automatically" and do a "clean project", and a Eclipse restart.
Clean your project or restart the eclipse.
Look to the imports, the R imported maybe is not correct.
If anything is wrong in your layouts, new updates aren't added to the R file. Look at the errors panel, and possibly to a clean rebuild to see what errors are popping up.
if u have an error in any of the resource files R will show up red unless all Errors are fixed and the project is built

Android App Error unknownpackageerror

I am creating simple android application in android of hello world printing and getting an -- error Description
Resource Path Location Type Error generating final archive: java.lang.ArrayIndexOutOfBoundsException: 13 Demo_ABC Unknown Android Packaging Problem
Code is:
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);
}
}
Someone else had the same problem with eclipse. The solution was to check the box "Create Test Project":
Your project is getting associated with some Test Project which you are not mapping with your newly created project. When you are creating any new project check carefully whether you are including the "Create Test Project" option or not.
Another guess: As the error seems to be package-related and your code does not include that, insert the correct package yourpackage.tld; in the code. (you said "Code is:").
Maybe that is missing Android expects it by default and gives weird errors because that was totally unforseen by the Google Android developers.

Missing R.java on Linux Eclipse Android project

I am running Eclipse Indigo with Android SDK 4.0.3 and Google API in Linux 11.10. However, when I start up a new Android project, my R.java is no where to be found, hence resulting in the following issue:
package com.lol.asdf;
import android.app.Activity;
import android.os.Bundle;
public class AsdfActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
I have tried all suggestion on this page but it's no good.
Anyone have any ideas?
your R.java is an auto generated class from the res/ folder. Check for error inside it. Try clean and rebuild your project too.
R.java will generated automatically...
first solve all other errors of your project it will be created...

How to fix NoClassFoundError in android

I have two classes in a different projects.
I am importing the project that has the class that I need.
Now, I have no errors.
The project know's that class and I even made an object from that class, but when I'm trying to run it it crushes with the "NoClassFoundError".
How can i fix that?
import com.dmg.pixelservice.core.*;
import android.app.Activity;
import android.os.Bundle;
public class show extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Pixel pixel = new Pixel();
pixel.doIt(this,"fd7ccf36-3f85-11e1-8671-40409e0f44a1",true);
setContentView(R.layout.main);
}
}
When I debug, I can see that it crashes when I'm trying to do Pixel pixel = new Pixel();
Please help.
create new libs folder in your application .name must be same.and put your jar files in this folder.and
go to
java Build Path -> Configure Build Path -> Add jars
Looks like the jar file containing the Pixel class is not packaged into the APK.
To make that happen, it seems you need to copy the jar into the libs folder of your Android project.
See this question:
Approach for fixing NoClassDefFoundError?
Android Developer Guide:
http://developer.android.com/guide/developing/projects
it is easy to find the error if you send the src code of your Pixel class.

Categories

Resources