java - 安卓程序无法运行

标签 java android

我是 Android 开发新手。我正在尝试创建一个 tic tac toe 游戏,目前我希望我的按钮在单击时标有 X,但是当我运行程序并单击按钮时什么也没有发生。我的 Activity.java 文件如下:

        package com.android.tictactoe;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class TictactoeActivity extends Activity implements Button.OnClickListener{
    Button one, two, three, four, five, six, seven, eight, nine;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        one=(Button)findViewById(R.id.one);
        one.setOnClickListener(this);
        two=(Button)findViewById(R.id.two);
        two.setOnClickListener(this);
        three=(Button)findViewById(R.id.three);
        three.setOnClickListener(this);
        four=(Button)findViewById(R.id.four);
        four.setOnClickListener(this);
        five=(Button)findViewById(R.id.five);
        five.setOnClickListener(this);
        six=(Button)findViewById(R.id.six);
        six.setOnClickListener(this);
        seven=(Button)findViewById(R.id.seven);
        seven.setOnClickListener(this);
        eight=(Button)findViewById(R.id.eight);
        eight.setOnClickListener(this);
        nine=(Button)findViewById(R.id.nine);
        nine.setOnClickListener(this);
    }
public void OnClick(Button button, int CheckedId){
    if(CheckedId==R.id.one && button.getText().equals("")){
        button.setText("X");
    }

    else if(CheckedId==R.id.two && button.getText().equals("")){
        button.setText("X");
    }

    else if(CheckedId==R.id.three && button.getText().equals("")){
        button.setText("X");
    }

    else if(CheckedId==R.id.four && button.getText().equals("")){
        button.setText("X");
    }

    else if(CheckedId==R.id.five && button.getText().equals("")){
        button.setText("X");
    }

    else if(CheckedId==R.id.six && button.getText().equals("")){
        button.setText("X");
    }

    else if(CheckedId==R.id.seven && button.getText().equals("")){
        button.setText("X");
    }

    else if(CheckedId==R.id.eight && button.getText().equals("")){
        button.setText("X");
    }

        button.setText("X");

}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

我的 main.xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="0" >

    <TableRow>
        <TextView
          android:text="Fizzle TicTacToe"
          android:textStyle="bold"         
            android:textSize="30dip"
            android:textColor="#ffaa88"

        />
        </TableRow>
    <TableRow>        
<Button
    android:textColor="#440000"
   android:textSize="40dip"
    android:textStyle="bold"
    android:text=""

     android:id="@+id/one"
android:layout_span="1" 
android:layout_width="0dip"
android:layout_height="100dip"

android:layout_weight="30"/>
<Button
      android:textColor="#440000"
   android:textSize="40dip"
    android:textStyle="bold"
     android:text=""


     android:id="@+id/two"
     android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
      android:textColor="#440000"
   android:textSize="40dip"
    android:textStyle="bold"
     android:text=""


     android:id="@+id/three"
android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
</TableRow>

    <TableRow>        
<Button
     android:textColor="#440000"
   android:textSize="40dip"
    android:textStyle="bold"
    android:text=""


     android:id="@+id/four"
android:layout_span="1" 
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
      android:textColor="#440000"
   android:textSize="40dip"
    android:textStyle="bold"
    android:text=""


     android:id="@+id/five"
     android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
      android:textColor="#440000"
   android:textSize="40dip"
    android:textStyle="bold"
    android:text=""

     android:id="@+id/six"
android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
</TableRow>

    <TableRow>        
<Button
      android:textColor="#440000"
   android:textSize="40dip"
    android:textStyle="bold"
    android:text=""


     android:id="@+id/seven"
android:layout_span="1" 
android:layout_width="0dip"
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
     android:textColor="#440000"
   android:textSize="40dip"
    android:textStyle="bold"
     android:text=""


     android:id="@+id/eight"
     android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
<Button
      android:textColor="#440000"
   android:textSize="40dip"
    android:textStyle="bold"
     android:text=""

     android:id="@+id/nine"
android:layout_span="1" 
android:layout_width="0dip" 
android:layout_height="100dip"
android:layout_weight="30"/>
</TableRow>


</TableLayout>

最佳答案

您需要在重写的方法而不是您自己的方法上提供 onClick 行为。

@Override
public void onClick(View v) {

    OnClick((Button)v, v.getId());
}

关于java - 安卓程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31099766/

相关文章:

java - 在 onLowMemory 方法中调用 Runtime.gc()

java - 在Java中,变量应该在函数的顶部声明,还是在需要时声明?

java - 如何在 JButton 中正确对齐多行文本?

java - Kotlin 中的 CountDownTimer 解释

android - 如何取消特定的警报?

java - 使用 AtomicReference 对多个预期值进行比较和设置

java - ArrayList 数组类型不匹配

android - 使用超过 "hue"设置 defaultMarker 的颜色

android - 更改 ANR(应用程序未响应)对话框的自定义对话框 (Android)

android - Svg 在设备中不可见但在 android xml 中可见