-7

我正在尝试编写井字游戏/应用程序,但无法正常工作。这段代码有什么问题?它在启动时崩溃。你能帮我么???

package com.example.tictactoe;

import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    // Spremenljivke
    int width, height;
    int s1, s2;// score
    int pot;// Poteza
    int round;// Runda
    int i;// for...
    Button b1, b2, b3, b4, b5, b6, b7, b8, b9;
    TextView rez1, rez2, runda;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        s1 = 0;
        s2 = 0;

        /*DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        height = metrics.heightPixels;
        width = metrics.widthPixels;*/

        b1.findViewById(R.id.button1);
        b2.findViewById(R.id.button2);
        b3.findViewById(R.id.button3);
        b4.findViewById(R.id.button4);
        b5.findViewById(R.id.button5);
        b6.findViewById(R.id.button6);
        b7.findViewById(R.id.button7);
        b8.findViewById(R.id.button8);
        b9.findViewById(R.id.button9);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        b4.setOnClickListener(this);
        b5.setOnClickListener(this);
        b6.setOnClickListener(this);
        b7.setOnClickListener(this);
        b8.setOnClickListener(this);
        b9.setOnClickListener(this);


          rez1.findViewById(R.id.textView1); rez2.findViewById(R.id.textView3);
          runda.findViewById(R.id.textView2);
          rez1.setText(""+s1); rez2.setText(""+rez2); runda.setText(""+round);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

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

        switch(v.getId()){
        case R.id.button1:
            rez1.setText("inside");
            break;
        case R.id.button2:
            rez1.setText("inside");
            break;
        case R.id.button3:
            rez1.setText("inside");
            break;
        case R.id.button4:
            rez1.setText("inside");
            break;
        case R.id.button5:
            rez1.setText("inside");
            break;
        case R.id.button6:
            rez1.setText("inside");
            break;
        case R.id.button7:
            rez1.setText("inside");
            break;
        case R.id.button8:
            rez1.setText("inside");
            break;
        case R.id.button9:
            rez1.setText("inside");
            break;
        }
    }

}
4

2 回答 2

3

你这样声明你的按钮:

b1.findViewById(R.id.button1);
        b2.findViewById(R.id.button2);
        b3.findViewById(R.id.button3);
        b4.findViewById(R.id.button4);
        b5.findViewById(R.id.button5);
        b6.findViewById(R.id.button6);
        b7.findViewById(R.id.button7);
        b8.findViewById(R.id.button8);
        b9.findViewById(R.id.button9);

将其替换为:

b1=(Button)findViewById(R.id.button1);

和其他类似的观点(b2,b3..,rez1,rez2..)。

于 2013-01-18T17:28:39.960 回答
2

您错误地分配了所有视图。代替:

b1.findViewById(R.id.button1);

利用:

b1 = (Button) findViewById(R.id.button1);

对于所有其他视图,依此类推。对于 TextViews 使用:

rez1 = (TextView) findViewById(R.id.textView1);

另一方面,你对setOnClickListener()and的使用是正确的,所以不要改变它。setText()

于 2013-01-18T17:25:39.150 回答