Cannot make a static reference to the non-static method getString(int) from the type Context

3k Views Asked by At

With reference to the lunchlist example in "Android PRogramming Tutorials" by Mark L.Murphy, In the below static class code (page 84) :

static class RestaurantHolder {
    private TextView name=null;
    private TextView address=null;
    private ImageView icon=null;
    RestaurantHolder(View row) {
        name=(TextView)row.findViewById(R.id.title);
        address=(TextView)row.findViewById(R.id.address);
        icon=(ImageView)row.findViewById(R.id.icon);
    }
   void populateFrom(Restaurant r) {
        name.setText(r.getName());
        address.setText(r.getAddress());
        if (r.getType().equals("sit_down")) {
          icon.setImageResource(R.drawable.ball_red);
        }
        else if (r.getType().equals("take_out")) {
          icon.setImageResource(R.drawable.ball_yellow);
        }
        else {
          icon.setImageResource(R.drawable.ball_green);
        }
      }
    }

I'm trying to replace

r.getType().equals("take_out")

with

r.getType().equals(getString(R.string.TakeAway))

but I'm getting the error "Cannot make a static reference to the non-static method getString(int) from the type Context"

Sorry it's probably a silly question but i really need help.

2

There are 2 best solutions below

0
On BEST ANSWER

if you want to get string in such way, you can try something like this:

void populateFrom(Restaurant r,Context context)
{
//other code
r.getType().equals(context.getString(R.string.TakeAway))
//other code
}
1
On

As you define the class with the static keyword by default all the member variable and method which was define into this class that are static so non static method you cannot used into the class method you need any object to use that method or you can remove the static keyword from the class definition