简体   繁体   English

Android应用程序在启动前崩溃

[英]Android application crashes before start

Displaying two random numbers and counting scores for odd and even turns. 显示两个随机数并计算奇数和偶数回合的分数。 When one of it reaches to 100, the application stops. 当其中之一达到100时,应用程序停止。

This is my java file of application 这是我的应用程序的Java文件

public class Board_Play1 extends Activity {

int d=0,a=0,b=0,turn=2;
Random random = new Random();
EditText diceno = (EditText) findViewById(R.id.editText1);
EditText p1 = (EditText) findViewById(R.id.editText2);
EditText p2 = (EditText) findViewById(R.id.editText3);

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.board_play1);


    diceno.setText(String.valueOf(d));  
    p1.setText(String.valueOf(a));
    p2.setText(String.valueOf(b));

    while(a!=100 && b!=100)
    {
        if(turn%2==0)
        {
            Button button = (Button) findViewById(R.id.button1);
            button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Auto-generated method stub
                    d=random.nextInt(6)+1;
                    EditText diceno = (EditText) findViewById(R.id.editText1);
                    diceno.setText(String.valueOf(d));

                }
            }); 
        }
        else
        {

            d=random.nextInt(6)+1;
            diceno.setText(String.valueOf(d));
        }

    if(turn%2==0)
            a+=d;
        else
            b+=d;

    if(a>100)
        a-=d;
    if(b>100)
        b-=d;


        p1.setText(String.valueOf(a));
        p2.setText(String.valueOf(b));
        turn++;
    }
    a=0;b=0;

}


}

It doesn't open and give an error saying Unfortunately your app has stopped . 它没有打开,并提示错误, Unfortunately your app has stopped Why is this happening? 为什么会这样呢? What can I change? 我可以改变什么?

Move all this inside onCreate after setContentView setContentView之后将所有这些移动到onCreate

EditText diceno;
EditText p1;
EditText p2 ;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.board_play1);
diceno = (EditText) findViewById(R.id.editText1);
p1 = (EditText) findViewById(R.id.editText2);
p2 = (EditText) findViewById(R.id.editText3);

findViewById looks for a view in the current view hierarchy. findViewById在当前视图层次结构中查找视图。 You ned to set the content of the layout to the activity first and then initialize vies by using findViewById . 您需要先将布局的内容设置为活动,然后使用findViewById初始化vies。

Also there is no need to re- initialize editText in button onClick. 同样,也不需要在按钮onClick上重新初始化editText。 Get rid of the below in the same 摆脱以下相同

EditText diceno = (EditText) findViewById(R.id.editText1);

Also move this 也把这个

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

before the while loop. 在while循环之前。 no need to initialize everytime 无需每次都初始化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM