U
U
USBHawk2018-08-26 22:25:14
Java
USBHawk, 2018-08-26 22:25:14

Why does the app open and immediately close?

I broke my head ... I read and do everything according to the Android book for professionals, part 3 .... The application from the second chapter opens and immediately closes, both in the emulator and on a real phone. Everything compiles fine. Please help...
I get a message on my phone that the application has stopped

package com.example.arterm.geoquiz;

public class Questions {
    private int mTextResId;
    private boolean mAnswerTrue;

    public Questions(int mTextResId, boolean mAnswerTrue){
        mTextResId=mTextResId;
        mAnswerTrue=mAnswerTrue;

    }

    public int getTextResId() {
        return mTextResId;
    }

    public void setTextResId(int textResId) {
        mTextResId = textResId;
    }

    public boolean isAnswerTrue() {
        return mAnswerTrue;
    }

    public void setAnswerTrue(boolean answerTrue) {
        mAnswerTrue = answerTrue;
    }
}

package com.example.arterm.geoquiz;

import android.app.admin.DevicePolicyManager;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends AppCompatActivity {

    private Button mTrueButton;
    private Button mFalseButton;
    private Button mNextButton;
    private TextView mQuestionTextView;

    private Questions[] mQuestionsBank=new Questions[]{
            new Questions(R.string.question_moscow,true),
            new Questions(R.string.question_artem, true),
            new Questions(R.string.question_irina, true),
            new Questions(R.string.question_balosh,false),
    };

    private int mCurrentIndex=0;

    public QuizActivity() {
    }

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

      mQuestionTextView=(TextView) findViewById(R.id.question_text_view);


        mNextButton =(Button) findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex=(mCurrentIndex+1)%mQuestionsBank.length;
                int question = mQuestionsBank[mCurrentIndex].getTextResId();
                mQuestionTextView.setText(question);
            }
        });



        mTrueButton= (Button) findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(QuizActivity.this,
                        R.string.correct_toast,
                        Toast.LENGTH_SHORT).show();


            }
        });

        mFalseButton=(Button) findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                 Toast.makeText(QuizActivity.this,R.string.incorrect_toast,
                       Toast.LENGTH_SHORT).show();
            }
        });

        mNextButton =(Button) findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex=(mCurrentIndex+1)%mQuestionsBank.length;
                updateQuestion();
            }
        });
        updateQuestion();
    }
    private void updateQuestion(){
        int question = mQuestionsBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(question);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">



        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="24dp"
            android:id="@+id/question_text_view"/>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/true_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/true_button" />

            <Button
                android:id="@+id/false_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/false_button" />

        </LinearLayout>
    <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next_button" />

    </LinearLayout>

<resources>
    <string name="app_name">GeoQuiz</string>
    <string name="question_moscow">Moscow is the capital of Russia?</string>
    <string name="question_irina">Irina is the beutiful girl?</string>
    <string name="question_artem">Arterm is the crazy?</string>
    <string name="question_balosh">We are love Balashov?</string>
    <string name="true_button">True</string>
    <string name="false_button">False</string>
    <string name="next_button">Next</string>
    <string name="correct_toast">Correct!</string>
    <string name="incorrect_toast">Incorrect</string>
</resources>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
EVGENY T., 2018-08-27
@USBHawk

It seems to me that there is confusion.
Replace the name of the input variable or write it like this:
this.mTextResId=mTextResId;

E
Eugene, 2018-08-27
@klim76

I read and do everything according to the book Android for professionals 3 part....

go to "Android for beginners part 1" about debugging and logs there should be

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question