动态处理按钮视图

时间:2018-02-20 16:14:14

标签: java android

我正在尝试为二维数组按钮动态处理onClick侦听器的创建。我通过搜索按钮ID名称的资源来做到这一点,但是尽管按钮正确地用XML初始化,我仍然收到以下错误:

java.lang.NoSuchFieldException: No field button00 in class Landroid/widget/Button; (declaration of 'android.widget.Button' appears in /system/framework/framework.jar!classes2.dex

按钮ID如下:button00,button01,button02,button10 ...等对应于它们在3x3网格中的位置,有人能指出为什么这不起作用吗?

预先初始化2d按钮对象数组:

private boolean zeroTurn = false;
private Button[][] boardButtons = new Button[3][3];

以下是我的onCreate,用于安排听众:

for(int x = 0; x< this.boardButtons.length; x++){
    for(int y = 0; y< this.boardButtons[x].length; y++){
        try {
            String buttonname = new StringBuffer("button"+x+y).toString();
            boardButtons[x][y] = findViewById(getResId(buttonname,Button.class));
            boardButtons[x][y].setOnClickListener(new View.OnClickListener(){
                public void onClick(View view){
                    Button clickedB = (Button) view;
                    Log.d("clickconfirm", "clicked!");
                    clickedB.setText(zeroTurn ? R.string.t_o:R.string.t_x);
                    clickedB.setEnabled(false);
                    zeroTurn = !zeroTurn;
                }
            });
        } catch(Exception e){
            e.printStackTrace();
            Log.d("Fail", "automation failed");
        }


    }
}

getResID函数将字符串和类作为输入,并返回我正在搜索的ID:

 public static int getResId(String resName, Class<?> c) {

        try {
            Field idField = c.getDeclaredField(resName);
            return idField.getInt(idField);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

1 个答案:

答案 0 :(得分:0)

问题在于此代码:

$.ajax({
        type: "POST",
        url: "../ps/manageuser.php",
        dataType: "json",
        async: true,
        data: data,
        success: function(response) {
            row = response.row ;
            column = response.columns;

            var newScript = document.createElement('script');
            newScript.type = 'text/javascript';
            newScript.src = '../js/footable.js';
            newScript.onload = function() {
            $(document).ready(function() {
            console.log(row);
            console.log(column);                
                $('.tableUsersSecteur').footable({"columns": column, "rows": row});
                });
            };
            var head = document.getElementsByTagName("head")[0];
            head.appendChild(newScript);
        },
        error: function(response){
            console.log("error");
        }
    });   
boardButtons[x][y] = findViewById(getResId(buttonname,Button.class));

将其替换为:

public static int getResId(String resName, Class<?> c) {
    try {
        Field idField = c.getDeclaredField(resName);
        return idField.getInt(idField);
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}
boardButtons[x][y] = findViewById(getResId(this, buttonname));

这假定您的代码是在public static int getResId(Context context, String buttonname) { context.getResources().getIdentifier(buttonname, "id", context.getPackageName()); } 内执行的(这似乎可能是因为您调用了Activity)。如果没有,则需要将真实的findViewById()实例传递给Context,而不仅仅是getResId()

请注意,如果找不到资源ID,则会返回this而不是0。如果你需要 -1(你不应该),那么你可以改变它:

-1