应用程序在启动时崩溃。

时间:2013-01-18 17:21:22

标签: android button

我正在尝试编写一个Tic Tac Toe游戏/应用程序,但我无法让它工作。这段代码有什么问题?它在启动时崩溃了。你能帮我吗???

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;
        }
    }

}

2 个答案:

答案 0 :(得分: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 ..)一样。

答案 1 :(得分:2)

您错误地分配了所有观看次数。而不是:

b1.findViewById(R.id.button1);

使用:

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

等等所有其他观点。对于TextViews,请使用:

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

另一方面,您对setOnClickListener()setText()的使用是正确的,因此请勿更改。