Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中单击元素不会';t显示正确的值_Java_Android - Fatal编程技术网

Java 在Android中单击元素不会';t显示正确的值

Java 在Android中单击元素不会';t显示正确的值,java,android,Java,Android,如果这个代码看起来有点混乱(考虑到长度),我道歉;我想我应该把目前正在进行的一切都包括在我的节目中 我正在尝试为Android创建一个相当简单的Tic-Tac-Toe应用程序。到目前为止,我已经很好地设置了我的UI,因此有一个文本视图的“网格”。作为一种“调试”,我现在有了它,当点击TextView时,它应该在消息框中显示buttonId的值。现在,它为我单击的第一个元素显示了正确的赋值,但不管我以后单击什么,它总是只显示buttonID的第一个值。我试图调试它,但无法准确地找到一个点,它将拉旧

如果这个代码看起来有点混乱(考虑到长度),我道歉;我想我应该把目前正在进行的一切都包括在我的节目中

我正在尝试为Android创建一个相当简单的Tic-Tac-Toe应用程序。到目前为止,我已经很好地设置了我的UI,因此有一个文本视图的“网格”。作为一种“调试”,我现在有了它,当点击TextView时,它应该在消息框中显示buttonId的值。现在,它为我单击的第一个元素显示了正确的赋值,但不管我以后单击什么,它总是只显示buttonID的第一个值。我试图调试它,但无法准确地找到一个点,它将拉旧值(据我所知,它重新分配了值)

我很有可能遗漏了一些小东西,因为这是我的第一个Android项目(不管怎么说)。有人能帮我找到buttonId的不同值,或者指出我逻辑中的错误吗

代码:

package com.TicTacToe.app;

import com.TicTacToe.app.R;
//Other import statements

public class TicTacToe extends Activity {
public String player = "X";
public int ALERT_ID;
public int buttonId;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Sets up instances of UI elements
    final TextView playerText = (TextView)findViewById(R.id.CurrentPlayerDisp);
    final Button button = (Button) findViewById(R.id.SetPlayer);
    final TextView location1 = (TextView)findViewById(R.id.location1);
    final TextView location2 = (TextView)findViewById(R.id.location2);
    final TextView location3 = (TextView)findViewById(R.id.location3);
    final TextView location4 = (TextView)findViewById(R.id.location4);
    final TextView location5 = (TextView)findViewById(R.id.location5);
    final TextView location6 = (TextView)findViewById(R.id.location6);
    final TextView location7 = (TextView)findViewById(R.id.location7);
    final TextView location8 = (TextView)findViewById(R.id.location8);
    final TextView location9 = (TextView)findViewById(R.id.location9);


    playerText.setText(player);

    //Handlers for events
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            if (player.equals("X")){
                player = "O";
                playerText.setText(player);
            }
            else if(player.equals("O")){
                player = "X";
                playerText.setText(player);
            }

            //Sets up the dialog
            buttonId = 0;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);
        }
    });

    location1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 1;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 2;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 3;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 4;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 5;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 6;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 7;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 8;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });

    location9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Sets up the dialog
            buttonId = 9;
            ALERT_ID = 0;
            onCreateDialog(ALERT_ID);
            showDialog(ALERT_ID);

        }
    });
}

protected Dialog onCreateDialog(int id){
    String msgString = "You are on spot " + buttonId;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(msgString)
            .setCancelable(false)
            .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    }
                });
    AlertDialog alert = builder.create();
    return alert;
}

}

您不应该自己调用
onCreateDialog()
。当你调用
showDialog()
时,Android会为你做这件事

我确信它只调用一次
onCreateDialog()
,并在内部缓存该值,这就是为什么第一次只得到正确的值,其余的都得到相同的值

你必须想出另一种方法来做你想做的事。也许将不同的ID传递给
showDialog()
(请确保通过在
onCreateDialog()
中创建正确的对话框来处理该ID)是一种方法

另外,不要手动导入R