Cody Cody - 1 month ago 17
Java Question

Android: Cannot Resolve Method

I'm trying to program custom states for programatically generated buttons in my app using the process explained here, but i'm getting the "cannot resolve method" compiler error when I try to call a method in my custom button class. The method i'm trying to call is public so I'm not sure why it's not visible in the scope i'm trying to call it from. Thoughts?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.menu_fragment, container, false);
typeOneArray = new boolean[21];
typeTwoArray = new int[21];
//programmatic button generation
buttonHolder = (LinearLayout) v.findViewById(R.id.buttonLayoutParent);
buttonHolder.setGravity(Gravity.RIGHT);
for (int j = 0; j < 21; j++) {
final int iteration = j;
final Button btnTag = new CustomButton(getActivity()); //CUSTOM BUTTON INSTANTIATION
btnTag.setId(j);
btnTag.setBackgroundResource(R.drawable.custom_button);
buttonHolder.addView(btnTag);
btnTag.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) { //button was clicked
//clickCounter0++;
typeTwoArray[iteration] = typeTwoArray[iteration] + 1;
Handler handler = new Handler();
Runnable singleClickLogic = new Runnable() {
@Override
public void run() {
if (typeTwoArray[iteration] !=0) {
Toast.makeText(getActivity(), "single click", Toast.LENGTH_SHORT).show();
typeTwoArray[iteration] = 0;
}
}
};
//Double click
if (typeTwoArray[iteration] == 2) {
btnTag.setButtonTypeOne(true); //THIS IS WHERE THE ERROR HAPPENS

Toast.makeText(getActivity(), "double click", Toast.LENGTH_SHORT).show();
typeTwoArray[iteration] = 0;
}
//Single click
else if (typeTwoArray[iteration] == 1) {
handler.postDelayed(singleClickLogic, 250);
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {

}
return false;
}
});
}
return v;
}


CustomButton.java

public class CustomButton extends Button {
private boolean stateTwo = false;
private boolean stateOne = false;
private static final int[] STATE_ONE = {R.attr.state_one};
private static final int[] STATE_TWO = {R.attr.state_two};

public CustomButton(Context context) {
super(context);
}

public void setButtonTypeOne(boolean buttonState) {
stateOne = buttonState;
}
public void setButtonTypeTwo(boolean buttonState) {
stateTwo = buttonState;
}

@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
if (stateOne) {
mergeDrawableStates(drawableState, STATE_ONE);
}
if (stateTwo) {
mergeDrawableStates(drawableState, STATE_TWO);
}
return drawableState;
}


}

Answer

Though the btnTag instance is a CustomButton, you've declared btnTag as just a regular Button.

final Button btnTag = new CustomButton(getActivity());

Button doesn't have your custom methods, so the compiler can't find setButtonTypeOne().

You can either declare btnTag as your CustomButton type:

final CustomButton btnTag = ...

Or cast it to that type before calling the method on it:

((CustomButton) btnTag).setButtonTypeOne(true);
Comments