Android Studio上的黑屏

时间:2018-09-13 00:39:55

标签: java android

嗨,这是我要修复的旧应用程序之一,运行此代码时出现黑屏,但该应用程序无法关闭。没有错误,字体也很好,为什么它不起作用?直到我开始与此课程的新活动之前,它都会起作用。提前致谢。另外,如果您能够建议一个更好的计时器来使用,那将非常好,因为当我以前使用此应用程序时,它无法正常工作。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:gravity="end">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/MainQuestion"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/SecondsLeft"
android:layout_gravity="center_horizontal"
/>
<Button
android:layout_width="115dp"
android:layout_height="87dp"
android:id="@+id/Question1"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="115dp"
android:layout_height="87dp"

android:id="@+id/Question2"
android:text=""
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="115dp"
android:layout_height="87dp"
android:id="@+id/Choice3"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="115dp"
android:layout_height="87dp"
android:id="@+id/Choice4"
android:layout_gravity="center_horizontal" />
</LinearLayout>

package com.steam.mytriviaapp;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.*;
import android.os.CountDownTimer;
import android.content.Intent;















//necessary imports//
public class SportsCat extends AppCompatActivity {//extension needed for app//
    TextView Question;//new textview variable to show the random question//
    Button Choice1;//new button variable for answers//
    Button Choice2;//^//
    Button Choice3;//^//
    Button Choice4;//^//
    TextView SecondsLeft;//new textview variable//
    int QuestionCounter =0;//question counter int set to 0//
    public static Integer Points = 0;// points set/reset to 0 depending on if it is first game since it was opened //
    boolean[] numbers = new boolean [4];//new boolean array with 4 numbers and are auto set to false, then later if true, they cannot be used, so it will not use samequestion//
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sports_cat);//all of these linked to their respective xml variable to change sizing etc//
        SecondsLeft = findViewById(R.id.SecondsLeft);
        Question = findViewById(R.id.MainQuestion);

        Choice1 = findViewById(R.id.Question1);
        Choice2 = findViewById(R.id.Question2);
        Choice3 = findViewById(R.id.Choice3);
        Choice4 = findViewById(R.id.Choice4);
        for(int k=0;k<=4;k++)
        {
            Body();
        }

    }
    public void Body() {//new object//
        String[] ArraySportsQuestions = {"Which NBA team holds the record for the most season wins in the Eastern Conference in history?", 
                "Which of these soccer players has won the most FIFA Ballon d'Ors after 2010?",
"Since what year was table tennis an Olympic sport?", "Which of these athletes had a video game made in honour of him?", "How many national sports does Canada have?"};//questions to be used//
        final String[] ArraySportsQ1 = {"Boston Celtics", "Miami Heat", "Chicago Bulls", "Cleveland Cavaliers"};//question answers//
        final String[] ArraySportsQ2 = {"Lionel Messi", "Cristiano Ronaldo", "Neymar", "Luis Suarez"};
        final String[] ArraySportsQ3 = {"1988", "1974", "1984", "1996"};
        final String[] ArraySportsQ4 = {"Shaun White", "Micheal Jordan", "Lionel Messi", "Shaun Federer"};
        final String[] ArraySportsQ5 = {"2", "1", "3", "4"};
        String[][] arrayofQs = {ArraySportsQ1, ArraySportsQ2, ArraySportsQ3,
                ArraySportsQ4, ArraySportsQ5};//2D array//
        QuestionCounter++;
        Random gen = new Random();//new random generator gen//
        int num = gen.nextInt(4);
        while(numbers[num] = true)//makes sure it is not the same number//
        {
            num = gen.nextInt(4);
        }
        numbers[num] = true;
        int x = gen.nextInt(3);
        int y = gen.nextInt(3);
        while(y==x) //^//
        {
            y = gen.nextInt(3);
        }
        int z = gen.nextInt(3);
        while(z==x||z==y) {
            z = gen.nextInt(3);
        }
        int u = gen.nextInt(3);
        while(u==x||u==y||u==z) {
            u = gen.nextInt(3);
        }
        new CountDownTimer(10000, 1000) {//android made countdown timer that icustomised//
            public void onTick(long millisUntilFinished) {

                SecondsLeft.setText("seconds remaining: " + millisUntilFinished /
                        1000);
            }
            public void onFinish() {
                SecondsLeft.setText("done!");
            }
        }.start();
        Typeface font2 = Typeface.createFromAsset(getAssets(), "subway.ttf");
        Question.setTypeface(font2);
        Question.setText(ArraySportsQuestions[num]);//randomising of questions andanswers//
        Choice1.setText(arrayofQs[num][x]);//sets the answer to the set for thequestion, for example is num = 3, answer set 3, and then the buttons below to randomstrings in this inner array//
        Choice2.setText(arrayofQs[num][y]);
        Choice3.setText(arrayofQs[num][z]);
        Choice4.setText(arrayofQs[num][u]);
Choice1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(Choice1.getText().equals(ArraySportsQ1[1])||Choice1.getText().equals(ArraySportsQ2[
                    1]) || Choice1.getText().equals(ArraySportsQ3[1]) ||
                    Choice1.getText().equals(ArraySportsQ4[1]) ||
                    Choice1.getText().equals(ArraySportsQ5[1]))// these are the answers//
            {
                Points++;
                Toast.makeText(SportsCat.this, "Correct!" ,
                Toast.LENGTH_LONG).show();//a notification type message at the bottom of the screendisappearing after//
                if(QuestionCounter==4)
                {
                    Intent j = new Intent(getApplicationContext(),gamedone.class);
                    startActivity(j);
                }else{
                    Body();
                }
            }else
            {
                Toast.makeText(SportsCat.this, "Incorrect" ,
                Toast.LENGTH_LONG).show();
                if(QuestionCounter==4)
                {
                    Intent j = new Intent(getApplicationContext(),gamedone.class);
                    startActivity(j);
                }else
                {
                    Body();
                }
            }

        }
    });
    Choice2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(Choice1.getText().equals(ArraySportsQ1[1])||Choice1.getText().equals(ArraySportsQ2[
                    1]) || Choice1.getText().equals(ArraySportsQ3[1]) ||
                    Choice1.getText().equals(ArraySportsQ4[1]) ||
                    Choice1.getText().equals(ArraySportsQ5[1]))
            {Points++;
                Toast.makeText(SportsCat.this, "Correct!" ,
                Toast.LENGTH_LONG).show();
                if(QuestionCounter==4)
                {
                    Intent j = new Intent(getApplicationContext(),gamedone.class);
                    startActivity(j);
                }else{
                    Body();
                }
            }else
            {
                Toast.makeText(SportsCat.this, "Incorrect" ,
                Toast.LENGTH_LONG).show();
                if(QuestionCounter==4)
                {
                    Intent j = new Intent(getApplicationContext(),gamedone.class);
                    startActivity(j);
                }else{
                    Body();
                }
            }
        }
    });
    Choice3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(Choice1.getText().equals(ArraySportsQ1[1])||Choice1.getText().equals(ArraySportsQ2[
                    1]) || Choice1.getText().equals(ArraySportsQ3[1]) ||
                    Choice1.getText().equals(ArraySportsQ4[1]) ||
                    Choice1.getText().equals(ArraySportsQ5[1]))
            {Points++;
                Toast.makeText(SportsCat.this, "Correct!" ,
                Toast.LENGTH_LONG).show();
                if(QuestionCounter==4)
                {
                    Intent j = new Intent(getApplicationContext(),gamedone.class);
                    startActivity(j);
                }else{
                    Body();
                }
            }else
            {
                Toast.makeText(SportsCat.this, "Incorrect" ,
                Toast.LENGTH_LONG).show();
                if(QuestionCounter==4)
                {
                    Intent j = new Intent(getApplicationContext(),gamedone.class);
                    startActivity(j);
                }else{

                    Body();
                }
            }
        }
    });
    Choice4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(Choice1.getText().equals(ArraySportsQ1[1])||Choice1.getText().equals(ArraySportsQ2[
                    1]) || Choice1.getText().equals(ArraySportsQ3[1]) ||
                    Choice1.getText().equals(ArraySportsQ4[1]) ||
                    Choice1.getText().equals(ArraySportsQ5[1]))
            {Points++;
                Toast.makeText(SportsCat.this, "Correct!" ,
                Toast.LENGTH_LONG).show();
                if(QuestionCounter==4)
                {
                    Intent j = new Intent(getApplicationContext(),gamedone.class);
                    startActivity(j);
                }else{
                    Body();
                }}else
            {
                Toast.makeText(SportsCat.this, "Incorrect" ,
                Toast.LENGTH_LONG).show();
                if(QuestionCounter==4)
                {
                    Intent j = new Intent(getApplicationContext(),gamedone.class);
                    startActivity(j);
                }else{
                    Body();
                }
                Toast.makeText(SportsCat.this, "Incorrect" ,
                Toast.LENGTH_LONG).show();
            }
        }
    });

}}

0 个答案:

没有答案