0

enter image description here

Hello everyone.

I've posted a basic diagram of my Android project above. It's pretty sad but that's what 5 minutes in paint get you.

Anyway, I'll walk you through it. I have xml with a series of imageViews which have onClickListeners in my board.Java class. If one of the imageViews are clicked on, an instance of pawn.java is instantiated, I pass the context to the instantiated pawn object, then call its possibleMoves() method.

At the end of this method I generate a list of int's which happen to be the id's of the imageViews. the final portion of possibleMoves() is the following:

for (String s : chunks) {                                                                                       
    String possibleSquare = "s" + s.substring(2, 4);                                                            
    Toast.makeText(boardContext, possibleSquare, Toast.LENGTH_SHORT).show();                                    
    int id = boardContext.getResources().getIdentifier(possibleSquare, "id", boardContext.getPackageName());    
    System.out.println(id);                                                                                     
    ImageView backgroundImg = (ImageView) findViewById(id);                                                     
    backgroundImg.setBackgroundColor(Color.rgb(255, 255, 255));                                                 

}                                                                                                               
return list;

The issue I'm having is that AndroidStudio says my findViewById(id) cannot be resolved. I've tried putting the context boardContext (the context I pass to my instantiated pawn object) in front of the findViewById, and I've tried using findViewById(R.id.id).

Suggestions?

credo56
  • 423
  • 1
  • 8
  • 19
  • Can you post the entire class where that section of code is being called? you are likely calling it somewhere where findViewById() does not exist – Joe Maher Mar 07 '15 at 03:15
  • Check [this](http://stackoverflow.com/questions/5498669/android-needing-context-in-non-activity-classes) out regarding passing context to non-activity classes. – Voicu Mar 07 '15 at 03:16
  • Also it looks like board does not have findViewById either, so using its context wont help in this situation, you need to use the activity that these classes are within – Joe Maher Mar 07 '15 at 03:16
  • You could pass the parent container and use it to find tour declared ImageView: `mParentContainer.findViewById(...)`. – Vikram Mar 07 '15 at 03:21

1 Answers1

2

findViewById(int id) are functions for View objects - here.. so you can not call it on nothing or any object. When you have reference to an Activity and you call findViewById(int id) it pulls the activity's contentView and calls it on it..

so your to your solution, Inflate the View containing your ImageView or get reference to your activity or if your context that you are passing is an Activity as context then you can cast your activity to the context and call your prefered method

Elltz
  • 10,730
  • 4
  • 31
  • 59