0

I have been using Eclipse and imported an old project, but got the following error:

R cannot be resolved to a variable

So I added: import android.R; and now I get all these errors:

main cannot be resolved or is not a field theme cannot be resolved or is not a field IVDisplay cannot be resolved or is not a field IVimage1 cannot be resolved or is not a field bSetWallpaper cannot be resolved or is not a field

Please if you could let me know what I am doing wrong.

Java:

package com.vamp6x6x6x.rusty;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

public class rustyactivity extends Activity implements MediaPlayer.OnCompletionListener, View.OnClickListener {
    /** Called when the activity is first created. */

    ImageView display;
    int toPhone;





    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        Button ending = (Button) findViewById(R.id.theme);
        ending.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playSound(R.raw.theme);
            }       
 });

        display = (ImageView) findViewById(R.id.IVDisplay);
        ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
        ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
        ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
        ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
        ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
         Button setWall = (Button) findViewById(R.id.bSetWallpaper);

        toPhone = R.drawable.guy1;

        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
        image4.setOnClickListener(this);
        image5.setOnClickListener(this);
        setWall.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {

        case R.id.IVimage1:
        display.setImageResource(R.drawable.guy1);
        toPhone = R.drawable.guy1;
        break;
        case R.id.IVimage2:
            display.setImageResource(R.drawable.guy2);
            toPhone = R.drawable.guy2;
            break;
        case R.id.IVimage3:
            display.setImageResource(R.drawable.guy3);
            toPhone = R.drawable.guy3;
            break;
        case R.id.IVimage4:
            display.setImageResource(R.drawable.guy4);
            toPhone = R.drawable.guy4;
            break;
        case R.id.IVimage5:
            display.setImageResource(R.drawable.guy5);
            toPhone = R.drawable.guy5;
            break;

        case R.id.bSetWallpaper:

            Bitmap whatever = BitmapFactory.decodeStream(getResources().openRawResource(toPhone));
               try{
                   getApplicationContext().setWallpaper(whatever);
               }catch(IOException e){
                   e.printStackTrace();
               }
        }


    }
        private void playSound(int resId) {
            MediaPlayer mp = MediaPlayer.create(this, resId);
            mp.setOnCompletionListener(this);
            mp.start(); 

        }  

        @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub

    }
    }

Main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.vamp6x6x6x.rusty"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


    <ImageView android:id="@+id/IVDisplay" android:src="@drawable/guy1" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center"></ImageView>
    <Button android:text="Set Wallpaper" android:id="@+id/bSetWallpaper"
        android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>

    <HorizontalScrollView android:layout_width="250dp" android:layout_height="wrap_content" android:layout_gravity="center">

    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">

    <ImageView android:id="@+id/IVimage1"
    android:src="@drawable/guy1"
        android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp"></ImageView>
<ImageView android:id="@+id/IVimage2"
    android:src="@drawable/guy2"
        android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp"></ImageView>
<ImageView android:id="@+id/IVimage3"
    android:src="@drawable/guy3"
        android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp"></ImageView>
<ImageView android:id="@+id/IVimage4"
    android:src="@drawable/guy4"
        android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp"></ImageView>
<ImageView android:id="@+id/IVimage5"
    android:src="@drawable/guy5"
        android:layout_width="125dp" android:layout_height="125dp" android:padding="15dp"></ImageView>

</LinearLayout>
</HorizontalScrollView>
    <Button android:layout_width="wrap_content" android:layout_gravity="center" android:id="@+id/theme" android:layout_height="wrap_content" android:text="Big Guy and Rusty the Boy Robot"></Button>

</LinearLayout>
vamp6x6x6x
  • 65
  • 1
  • 2
  • 8

3 Answers3

1

Remove the Android.r and do a clean then a build.

yams
  • 942
  • 6
  • 27
  • 60
1

Usually Eclipse has trouble generating the R file when there is a problem in some of your resources (xmls, images etc...)

The R that is missing is not android.R, but the R file that maps your project's resources.

You should look at your console (window->show view->Console) and see if you get errors, and if you see nothing, you should check out the LogCat logs in the DDMS perspective

dors
  • 5,802
  • 8
  • 45
  • 71
1

I have encountered this problem a couple of times. The first time it turned out to be an error in my one of my XML layout files. The second time was when I decided to change my projects namespace.

This seems to be a common problem people encounter at some point- I found this link useful.

Hope it helps.

Community
  • 1
  • 1
MillerTime
  • 61
  • 2