Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

lincoln bernard
lincoln bernard
74 Points

In Eclipse keep getting error "R cannot be resolved as a variable"

In an array adapter I keep getting the error "R cannot be resolved as a variable" and I have know idea why this is happening and all I could understand from a quick google search is that R is a file automatically generated that shouldn't be causing any errors. I have tried refreshing and cleaning my project and that hasn't worked and the R.java file is definitely present in the "ten" folder. Here is the code that is giving me errors.

package com.bernard.beaconportal.adaptor;

import com.bernard.beaconportal.R;

import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView;

public class MobileArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[] values;

public MobileArrayAdapter(Context context, String[] values) {
    super(context, R.layout.activity_homeworkdue_twopane, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.activity_homeworkdue_twopane, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageview = (ImageView) rowView.findViewById(R.id.logo);
    textView.setText(values[position]);

    // Change icon based on name
    String s = values[position];

    System.out.println(s);

    if (s.equals("1")) {
        imageview.setImageResource(R.drawable.english);
    } else if (s.equals("2")) {
        imageview.setImageResource(R.drawable.mathematics);
    } else if (s.equals("3")) {
        imageview.setImageResource(R.drawable.global_studies);
    } else if (s.equals("5")) {
        imageview.setImageResource(R.drawable.science);
    } else {
        imageview.setImageResource(R.drawable.half_life_3_where_are_you);
    }

    return rowView;
}

}

4 Answers

Pradeep S
PLUS
Pradeep S
Courses Plus Student 1,003 Points

Did you update ADT? If yes, Could you please update your build tools and check again? It will be useful if you can post the versions

lincoln bernard
lincoln bernard
74 Points

Thank you for the reply It seems I have not installed the build tools for sdk 19.0.1 so I will install those and see if it works than.

lincoln bernard
lincoln bernard
74 Points

That worked, turns out there was a small update (19.0.1) that was released. Thank You!