Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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程序不工作_Java_Android - Fatal编程技术网

Java Android程序不工作

Java Android程序不工作,java,android,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.*; publ

我是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

}

}
<?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>
my main.xml如下所示:

        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

}

}
<?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());
}

您的OnClick方法无效,请使用overide将其内容复制到OnClick方法