按钮无法正常工作

时间:2018-01-23 19:56:43

标签: android

我为我的应用程序创建了一个android studio按钮,当我点击播放游戏按钮时,它不起作用。我没有得到任何错误,它只是不起作用。当用户点击播放游戏按钮时,我想进入测验活动......

HomeScreen.java

package com.littlekidsmath.yoong.mathlearningforkids;

import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import info.hoang8f.widget.FButton;

public class HomeScreen extends AppCompatActivity {
FButton playGame,quit;
TextView tQ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
    playGame =(FButton)findViewById(R.id.playGame);
    quit = (FButton) findViewById(R.id.quit);
    tQ = (TextView)findViewById(R.id.tQ);
    //PlayGame button - it will take you to the MainGameActivity
    playGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new     Intent(HomeScreen.this,MainGameActivity.class);
            startActivity(intent);

        }
    });
    //Quit button - This will quit the game
    quit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });

    //Typeface - this is for fonts style
    Typeface typeface =     Typeface.createFromAsset(getAssets(),"fonts/shablagooital.ttf");
    playGame.setTypeface(typeface);
    quit.setTypeface(typeface);
    tQ.setTypeface(typeface);
}

}

activity home screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fbutton="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@color/backgroundColor"
tools:context="com.littlekidsmath.yoong.mathlearningforkids.HomeScreen">
<TextView
android:id="@+id/tQ"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:gravity="center"
android:textSize="50sp"/>
<View
android:layout_width="match_parent"
android:layout_height="20dp"></View>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/triviafinal"/>

<info.hoang8f.widget.FButton
    android:id="@+id/playGame"
    fbutton:buttonColor="@color/lightGreen"
    android:layout_width="300dp"
    android:text="@string/play_game"
    android:layout_height="wrap_content" />

<View
    android:layout_width="match_parent"
    android:layout_height="40dp"></View>


<info.hoang8f.widget.FButton
    android:id="@+id/quit"
    fbutton:buttonColor="@color/lightGreen"
    android:layout_width="300dp"
    android:text="@string/quit"
    android:layout_height="wrap_content" />

MainGameActivity.java

package com.littlekidsmath.yoong.mathlearningforkids;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
import info.hoang8f.widget.FButton;



public class MainGameActivity extends AppCompatActivity {
FButton buttonA, buttonB, buttonC, buttonD;
TextView questionText, QuizText, timeText, resultText, coinText;
QuizHelper QuizHelper;
Question currentQuestion;
List<Question> list;
int qid = 0;
int timeValue = 20;
int coinValue = 0;
CountDownTimer countDownTimer;
Typeface tb, sb;

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

    //Initializing variables
    questionText = (TextView) findViewById(R.id.Question);
    buttonA = (FButton) findViewById(R.id.buttonA);
    buttonB = (FButton) findViewById(R.id.buttonB);
    buttonC = (FButton) findViewById(R.id.buttonC);
    buttonD = (FButton) findViewById(R.id.buttonD);
    QuizText = (TextView) findViewById(R.id.QuizText);
    timeText = (TextView) findViewById(R.id.timeText);
    resultText = (TextView) findViewById(R.id.resultText);
    coinText = (TextView) findViewById(R.id.coinText);

    //Setting typefaces for textview and buttons - this will give stylish fonts on textview and button etc
    tb = Typeface.createFromAsset(getAssets(), "fonts/TitilliumWeb-Bold.ttf");
    sb = Typeface.createFromAsset(getAssets(), "fonts/shablagooital.ttf");
    QuizText.setTypeface(sb);
    questionText.setTypeface(tb);
    buttonA.setTypeface(tb);
    buttonB.setTypeface(tb);
    buttonC.setTypeface(tb);
    buttonD.setTypeface(tb);
    timeText.setTypeface(tb);
    resultText.setTypeface(sb);
    coinText.setTypeface(tb);

    //Our database helper class
    QuizHelper = new QuizHelper(this);
    //Make db writable
    QuizHelper.getWritableDatabase();

    //It will check if the ques,options are already added in table or not
    //If they are not added then the getAllOfTheQuestions() will return a list of size zero
    if (QuizHelper.getAllOfTheQuestions().size() == 0) {
        //If not added then add the ques,options in table
        QuizHelper.allQuestion();
    }

    //This will return us a list of data type TriviaQuestion
    list = QuizHelper.getAllOfTheQuestions();

    //Now we gonna shuffle the elements of the list so that we will get questions randomly
    Collections.shuffle(list);

    //currentQuestion will hold the que, 4 option and ans for particular id
    currentQuestion = list.get(qid);

    //countDownTimer
    countDownTimer = new CountDownTimer(22000, 1000) {
        public void onTick(long millisUntilFinished) {

            //here you can have your logic to set text to timeText
            timeText.setText(String.valueOf(timeValue) + "\"");

            //With each iteration decrement the time by 1 sec
            timeValue -= 1;

            //This means the user is out of time so onFinished will called after this iteration
            if (timeValue == -1) {

                //Since user is out of time setText as time up
                resultText.setText(getString(R.string.timeup));

                //Since user is out of time he won't be able to click any buttons
                //therefore we will disable all four options buttons using this method
                disableButton();
            }
        }

        //Now user is out of time
        public void onFinish() {
            //We will navigate him to the time up activity using below method
            timeUp();
        }
    }.start();

    //This method will set the que and four options
    updateQueAndOptions();


}


public void updateQueAndOptions() {

    //This method will setText for que and options
    questionText.setText(currentQuestion.getQuestion());
    buttonA.setText(currentQuestion.getOptA());
    buttonB.setText(currentQuestion.getOptB());
    buttonC.setText(currentQuestion.getOptC());
    buttonD.setText(currentQuestion.getOptD());


    timeValue = 20;

    //Now since the user has ans correct just reset timer back for another que- by cancel and start
    countDownTimer.cancel();
    countDownTimer.start();

    //set the value of coin text
    coinText.setText(String.valueOf(coinValue));
    //Now since user has ans correct increment the coinvalue
    coinValue++;

}

//Onclick listener for first button
public void buttonA(View view) {
    //compare the option with the ans if yes then make button color green
    if (currentQuestion.getOptA().equals(currentQuestion.getAnswer())) {
        buttonA.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        //Check if user has not exceeds the que limit
        if (qid < list.size() - 1) {

            //Now disable all the option button since user ans is correct so
            //user won't be able to press another option button after pressing one button
            disableButton();

            //Show the dialog that ans is correct
            correctDialog();
        }
        //If user has exceeds the que limit just navigate him to GameWon activity
        else {

            gameWon();

        }
    }
    //User ans is wrong then just navigate him to the PlayAgain activity
    else {

        gameLostPlayAgain();

    }
}

//Onclick listener for sec button
public void buttonB(View view) {
    if (currentQuestion.getOptB().equals(currentQuestion.getAnswer())) {
        buttonB.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        if (qid < list.size() - 1) {
            disableButton();
            correctDialog();
        } else {
            gameWon();
        }
    } else {
        gameLostPlayAgain();
    }
}

//Onclick listener for third button
public void buttonC(View view) {
    if (currentQuestion.getOptC().equals(currentQuestion.getAnswer())) {
        buttonC.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        if (qid < list.size() - 1) {
            disableButton();
            correctDialog();
        } else {
            gameWon();
        }
    } else {

        gameLostPlayAgain();
    }
}

//Onclick listener for fourth button
public void buttonD(View view) {
    if (currentQuestion.getOptD().equals(currentQuestion.getAnswer())) {
        buttonD.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        if (qid < list.size() - 1) {
            disableButton();
            correctDialog();
        } else {
            gameWon();
        }
    } else {
        gameLostPlayAgain();
    }
}

//This method will navigate from current activity to GameWon
public void gameWon() {
    Intent intent = new Intent(this, GameWon.class);
    startActivity(intent);
    finish();
}

//This method is called when user ans is wrong
//this method will navigate user to the activity PlayAgain
public void gameLostPlayAgain() {
    Intent intent = new Intent(this, PlayAgain.class);
    startActivity(intent);
    finish();
}

//This method is called when time is up
//this method will navigate user to the activity Time_Up
public void timeUp() {
    Intent intent = new Intent(this, Time_Up.class);
    startActivity(intent);
    finish();
}

//If user press home button and come in the game from memory then this
//method will continue the timer from the previous time it left
@Override
protected void onRestart() {
    super.onRestart();
    countDownTimer.start();
}

//When activity is destroyed then this will cancel the timer
@Override
protected void onStop() {
    super.onStop();
    countDownTimer.cancel();
}

//This will pause the time
@Override
protected void onPause() {
    super.onPause();
    countDownTimer.cancel();
}

//On BackPressed
@Override
public void onBackPressed() {
    Intent intent = new Intent(this, HomeScreen.class);
    startActivity(intent);
    finish();
}

//This dialog is show to the user after he ans correct
public void correctDialog() {
    final Dialog dialogCorrect = new Dialog(MainGameActivity.this);
    dialogCorrect.requestWindowFeature(Window.FEATURE_NO_TITLE);
    if (dialogCorrect.getWindow() != null) {
        ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
        dialogCorrect.getWindow().setBackgroundDrawable(colorDrawable);
    }
    dialogCorrect.setContentView(R.layout.dialog_correct);
    dialogCorrect.setCancelable(false);
    dialogCorrect.show();

    //Since the dialog is show to user just pause the timer in background
    onPause();


    TextView correctText = (TextView) dialogCorrect.findViewById(R.id.correctText);
    FButton buttonNext = (FButton) dialogCorrect.findViewById(R.id.dialogNext);

    //Setting type faces
    correctText.setTypeface(sb);
    buttonNext.setTypeface(sb);

    //OnCLick listener to go next que
    buttonNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //This will dismiss the dialog
            dialogCorrect.dismiss();
            //it will increment the question number
            qid++;
            //get the que and 4 option and store in the currentQuestion
            currentQuestion = list.get(qid);
            //Now this method will set the new que and 4 options
            updateQueAndOptions();
            //reset the color of buttons back to white
            resetColor();
            //Enable button - remember we had disable them when user ans was correct in there particular button methods
            enableButton();
        }
    });
}


//This method will make button color white again since our one button color was turned green
public void resetColor() {
    buttonA.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
    buttonB.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
    buttonC.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
    buttonD.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
}

//This method will disable all the option button
public void disableButton() {
    buttonA.setEnabled(false);
    buttonB.setEnabled(false);
    buttonC.setEnabled(false);
    buttonD.setEnabled(false);
}

//This method will all enable the option buttons
public void enableButton() {
    buttonA.setEnabled(true);
    buttonB.setEnabled(true);
    buttonC.setEnabled(true);
    buttonD.setEnabled(true);
}

}

有人可以帮忙吗?单击播放游戏按钮时,应用程序将关闭。我希望它转到名为MainGameActivity.java的测验页面。我在Manifest中声明MainGameActivity已经无法正常工作。

这是Logcat

01-24 04:05:08.914 9833-9833/com.littlekidsmath.yoong.mathlearningforkids     I/art: Late-enabling -Xcheck:jni
01-24 04:05:09.194 9833-9833/com.littlekidsmath.yoong.mathlearningforkids     I/InstantRun: starting instant run server: is main process
01-24 04:05:09.364 9833-9833/com.littlekidsmath.yoong.mathlearningforkids     W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter     android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.    graphics.PorterDuffColorFilter, android.content.res.ColorStateList,     android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-    private method in android.graphics.drawable.Drawable
01-24 04:05:09.414 9833-9833/com.littlekidsmath.yoong.mathlearningforkids     E/Process: android_os_Process_getProcessNameByPid pid is 9833
01-24 04:05:09.414 9833-9833/com.littlekidsmath.yoong.mathlearningforkids E/Process: android_os_Process_getProcessNameByPid value is learningforkids
01-24 04:05:09.654 9833-9833/com.littlekidsmath.yoong.mathlearningforkids W/art: Before Android 4.1, method int     android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean)     would have incorrectly overridden the package-private method in     android.widget.ListView
01-24 04:05:09.664 9833-9833/com.littlekidsmath.yoong.mathlearningforkids     I/ListPopupWindow: Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.
01-24 04:05:09.704 9833-9856/com.littlekidsmath.yoong.mathlearningforkids D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-24 04:05:09.714 9833-9833/com.littlekidsmath.yoong.mathlearningforkids D/Atlas: Validating map...
01-24 04:05:09.774 9833-9856/com.littlekidsmath.yoong.mathlearningforkids I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BR.1.1.3.C8.05.01.00.115.128_msm8916_64_refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.3.C8.05.01.00.115.128__release_AU (I55c48cad9a)
                                                                                        OpenGL ES Shader Compiler Version: E031.25.03.04
                                                                                        Build Date: 03/21/17 Tue
                                                                                    Local Branch: 
                                                                                    Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.3.C8.05.01.00.115.128
                                                                                    Local Patches: NONE
                                                                                    Reconstruct Branch: NOTHING

01-24 04:05:09.774 9833-9856 / com.littlekidsmath.yoong.mathlearningforkids I / OpenGLRenderer:初始化的EGL,版本1.4 01-24 04:05:09.774 9833-9856 / com.littlekidsmath.yoong.mathlearningforkids D / OpenGLRenderer:启用调试模式0

0 个答案:

没有答案