【问题标题】:Application crashes at the startup. [closed]应用程序在启动时崩溃。 [关闭]
【发布时间】:2013-01-02 11:26:21
【问题描述】:

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

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

}

【问题讨论】:

  • 错误信息会有帮助
  • 我建议人们不介意帮助解决特定的家庭作业问题,但是没有特定问题的帖子不太可能得到积极的回应。

标签: android button


【解决方案1】:

你这样声明你的按钮:

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..)。

【讨论】:

    【解决方案2】:

    您错误地分配了所有视图。而不是:

    b1.findViewById(R.id.button1);
    

    用途:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 2016-09-02
      • 1970-01-01
      • 2012-09-15
      相关资源
      最近更新 更多