繁体   English   中英

Android:无法解析方法

[英]Android: Cannot Resolve Method

我正在尝试使用此处说明的过程在我的应用程序中为编程生成的按钮编写自定义状态,但是当我尝试在我的自定义按钮类中调用方法时,我得到了“无法解决方法”编译器错误。 我试图调用的方法是公共的,所以我不确定为什么它在我想要调用它的范围内不可见。 思考?

@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;
}

}

虽然btnTag实例是CustomButton ,但您已将btnTag声明为常规Button

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

Button没有自定义方法,因此编译器找不到setButtonTypeOne()

您可以将btnTag声明为CustomButton类型:

final CustomButton btnTag = ...

或者在调用方法之前将其强制转换为该类型:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM