• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps My first android project & R.java error

I'll make it quick with 3 screenshots.

1. I have eclipse 4.2.2 with ADT Plugin (23.0.4) & these sdk packages intalled. (Picture 1)

2. I created my first hello world project for android with these settings. (Picture 2)

3. I got this error, "R can't be resolved to a variable". (Picture 3)


image.png


image.png


image.png


Edit:
Java:
package com.terry.helloworld;

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);
   }
}
 
Last edited:
I've dabbled a wee bit with development but sadly probably not enough to be able to lend you any assistance personally. :(

I did find a note on stackoverflow that might be relevant:

*Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.*

There are some other suggestions posted on that thread that might also be worth a look: http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error

BTW, you can post code snippets wrapped in [code=java][/code] tags which might make it easier for folks to read.

Good luck! :D
 
Hmm... could you maybe also share your AndroidManifest.xml?

And did a R.java file get created correctly under the "gen" tree?
 
No, i didn't get R.java under gen tree at all.

Java:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.terry.helloworld"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
  android:minSdkVersion="9"
  android:targetSdkVersion="18" />

  <application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >
  <activity
  android:name=".MainActivity"
  android:label="@string/app_name" >
  <intent-filter>
  <action android:name="android.intent.action.MAIN" />

  <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
  </activity>
  </application>

</manifest>
 
Last edited:
Could you show a screenshot of what shows up under the /gen/ tree? From my n00bgrammer perspective, it looks like something is preventing that R.java class from being created (hence the "cannot resolve" error).

Most of the Internet seems to suggest that happens when (1) there is a problem with a resource or xml under /res/ or (2) you've renamed the project somewhere along the line. I'm not exactly sure how to track down the problem file under the first case, but I believe that doing a Project -> Clean in Eclipse should scrap the entire /gen/ tree and build a new one (which hopefully matches this time).
 
Doing Project - > Clean didn't solve the problem. BTW, i got the solution. I just downloaded API 21 files and set my target sdk to this latest one api. Thanx for help anyway guys. :)
 
^ That was the other scenario I had read about but completely forgot when posting. :D

Anyway, I'm glad that you got it sorted - and thanks a bunch for reporting back with the solution! Hopefully your experience will be able to help out someone else in the future. :)
 
I know you solved it already by changing your target sdk, but for future reference... This is just a bug with the ide, it also happens in Android Studio. I don't use Eclipse, but in AS you just resync the project with gradle and it fixes the issue right up.
 
Back
Top Bottom