我试着做一个简单的测验,让我们做一个30秒的简单数学问题的测验,然后显示分数

ryevplcw  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(274)

该应用程序的开始是通过使布局中的每一个其他的东西是不可行的或消失了(我不知道哪一个它应该-请推荐)。我已经做了按钮,它和其他每一个视图,但一旦我点击按钮,我的应用程序崩溃。这里是所有的java。

package com.example.braintimer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    GridLayout game;

    TextView timer;

    TextView problem;

    TextView result;
    TextView score;
    Button newGame;

    public void firstGo(View view) {
        Button firstGo = (Button) findViewById(R.id.firstGo);

       game  = (GridLayout) findViewById(R.id.gridLayout);

       timer  = (TextView) findViewById(R.id.timer);

       problem = (TextView) findViewById(R.id.problem);

       result  = (TextView) findViewById(R.id.result);

       score  = (TextView) findViewById(R.id.score);

       newGame  = (Button) findViewById(R.id.newGame);

        firstGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("1" , "Didnt crash");
                firstGo.setVisibility(View.GONE);

                game.setVisibility(View.VISIBLE);
                timer.setVisibility(View.VISIBLE);
                problem.setVisibility(View.VISIBLE);
                result.setVisibility(View.VISIBLE);
                score.setVisibility(View.VISIBLE);
                newGame.setVisibility(View.VISIBLE);
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}``` 
Please recommend what should be done to stop the app crashing. Also can anyone tell that how should I get the math questions i want for the app
Thank you
yvfmudvl

yvfmudvl1#

从您的代码中,我可以看到您没有从任何地方调用firstgo()方法。只需从oncreate()方法afetr secontenview()调用它,而不在参数中传递视图。
如下所示:

public class MainActivity extends AppCompatActivity {

GridLayout game;

TextView timer;

TextView problem;

TextView result;
TextView score;
Button newGame;

public void firstGo() {
    Button firstGo = (Button) findViewById(R.id.firstGo);

   game  = (GridLayout) findViewById(R.id.gridLayout);

   timer  = (TextView) findViewById(R.id.timer);

   problem = (TextView) findViewById(R.id.problem);

   result  = (TextView) findViewById(R.id.result);

   score  = (TextView) findViewById(R.id.score);

   newGame  = (Button) findViewById(R.id.newGame);

    firstGo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("1" , "Didnt crash");
            firstGo.setVisibility(View.GONE);

            game.setVisibility(View.VISIBLE);
            timer.setVisibility(View.VISIBLE);
            problem.setVisibility(View.VISIBLE);
            result.setVisibility(View.VISIBLE);
            score.setVisibility(View.VISIBLE);
            newGame.setVisibility(View.VISIBLE);
        }
    });
}
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    firstGo()
   }
}

相关问题