Android R & layout error - android

I am a beginner Android app developer. I created an XML layout called "splash" and I have changed setContentView(R.layout.activi­ty_main); to setContentView(R.layout.splash­); but got an error. I tried to do everything all over again but I got the same error in the R. When I did a "quick fix" a new error appears. It was caused by activity_main and splash.
When I started all over again, I just changed the background in activity_main but but I got the same error although I haven't modified anything in MainActivity.java.
Here is my code so far:
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;
}

You need to be careful when importing R because the Android OS provides one with system defaults etc.
Have a look in your imports to make sure you're getting the R from your package.
You might also see issues if your layout doesn't compile.

Related

Eclipse can't detect resources - "R cannot be resolved to a variable"

When I try to create an android project in eclipse the code has errors.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
I'm getting "R cannot be resolved to a variable"
I think it can't detect the resources?
My SRC code is:
package com.android.h4;
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);
}
}
In all of R eclipse detects an error. I tried to create a new project with appcompat_v7 and I deleted the libs and res folders as well as the AndroidManifest.xml. Then i imported these folder from a working project with the same name(someone emailed them to me) and at emptied the src but the error remains.
Any idea what I should do?
There are some possibilities that may evoke that error.
The easiest way (seems stupid but often helps)
Go to "Project" in the Menu of Eclipse and click on "Clean"
After Cleaning, the workspace is rebuild and hopefully the R-file
is recognized than. (Sometimes even a restart of Eclipse helps)
Check all your IDs. As already said in comments, it's possible that you have
misspelled the ID (you define your IDs in the layout, there you have to check it)
Last but not least: DON'T TRY TO CHANGE THE R-FILE YOURSELF! :)
I hope I could help you. If not, just specify your problem (i.e send a copy of your layout xml)
You should clean your project and build again. For doing that you should go to
tab project -> clean -> select the project
And for more details you can also dig out on these links
R cannot be resolved - Android error
“R cannot be resolved to a variable”? duplicate

Errors starting with Eclipse

I am new with JavaScript and Developement on Android. I installed Eclipse but I can't even run the "Hello World" program, included when you created a new project !
Please help me, I spent the two last days installing this IDE (even tried Android Studio),
but there is always the same error :
Could not find Chwet.apk!
And there seems to be an error with the "R", like in
setContentView(R.layout.activity_main)
Here is my code :
package com.example.chwet;
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.main, menu);
return true;
}
}
Look at following tutorial to get start with Android application development.
Hello world example.
Instead of importing android R class , import R class with your package name.
Press Ctrl+shift+o to add missing packages. It will automatically remove your errors.

Console of my Android Studio does not print the log message

I created a Hello, World project in Android Studio to test using System.out.println(). I believe the log message should be printed in the console, but it wasn't. Im using Android Studio AI-130.737825 with JRE:1.7.0_25. The test code follows:
package com.example.consoletest;
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);
System.out.println("please print me in the console");
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;
}
}
The console is not "connected" to the running app because it is run on a different system (be it an emulator, or physical device). The only "connected" part in Android Studio is the LogCat, which can be accessed using the Android tab at the bottom of the IDE.
You should rather print output to LogCat using the Log.* methods, which provides a lot more control and information, in almost the same simplistic way. Additionally, the logcat can be filtered to find exactly what you want.
As #free3om hinted at, Log.* can be used to print out many different outputs to the Logcat. If you want to see just errors, you can use Log.e(String s1, String s2) to see what and where something went wrong. Here's a link to the doc's for Log. http://developer.android.com/reference/android/util/Log.html

R cannot be resolved to a variable without doing anything to the project

I am getting the error "R cannot be resolved to a variable" that many people seems to be getting. However, none of the answers that I have seen has worked for me. I've gone to the extreme basic and created a helloworld project, with all default settings. The "R cannot be resolved to a variable" error is created instantly, and I cannot see what is wrong... no xml errors, all lowercase in /res/ folder, no difference in AndroidManifest.xml.
For reference, the code on MainActivity.java is:
package com.test.helloworld;
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.main, menu);
return true;
}
}
Is there something wrong with eclipse setting?
I'm using Windows 7 64-bit
Whenever I had a problem with R not been generated, or even disappeared, first clean and rebuild the project, if this is not worked then it was due to some problem in the XML layout file that prevented the application from being built. Check for the /res directory and there must be some file that have some error in it and that is preventing the application from being built. For example, it may be a layout file or it may be due to some missing resource is, but you already defined it in the XML file.

Android: R cannot be resolved to a variable

Ok, I know there are several questions like this, but I've been through all of them and nothing seems to work! I have been using eclipse for almost a year now. I installed new softwares from the Help menu, and since then nothing is working! I re-installed eclipse and now I'm getting this "R cannot be resolved to a variable", even when I create a brand new project. Here's the code:
package test.pro;
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.main, menu);
return true;
}
}
I am getting an error in the setContentView and getMenuInflater lines. I looked in the 'gen' folder, there's nothing there! I also checked the Java Build Path, it's fine. The ADT is updated. Can someone help me out here!!? I really need it!
Usually this error is because of some error in layout, strings, dimensions or any other xml file. Look for such xml error, resolve it and you should be good to go.
Also do perform a clean of your project. This error is very common with me too.

Categories

Resources