Grady
March 3, 2017, 1:25pm
1
The app crashes and the issue seems to be this line of code
mQuestionTextView.setText(question);
When I gray it out the app will run. I look at the similar issues on this forum but can’t figure it out.
package com.example.grady.geoquiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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;
//question array
private Question[] mQuestionBank = new Question[] {
new Question(R.string.question_australia, true),
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, true),
new Question(R.string.question_africa, true),
new Question(R.string.question_americas, true),
new Question(R.string.question_australia, true)
};
private int mCurrentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
//load the question
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
int question = mQuestionBank[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();
}
});
}
}
XML:
?xml version=“1.0” encoding=“utf-8”?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/question_text_view"
android:padding="24dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/true_button"
android:text="@string/true_button"/>
<Button
android:text="@string/false_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/false_button"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next_button"
android:text="@string/next_button"/>
NickD
April 1, 2017, 1:47am
2
Take a look at my code but ignore the life cycle stuff. Maybe it will help. Eventually you will need to increment and decrement the index so I mad separate methods for them. Let me know if this helps. Nick
package com.bignerdranch.android.geoquiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import static java.lang.System.*;
public class QuizActivity extends AppCompatActivity
{
private static final String TAG = “QuizActivity”;
// Make all the UI Wizards known in the Main Class
private TextView mQuestionTextView;
private ImageButton mPrevButton;
private ImageButton mNextButton;
private Button mTrueButton;
private Button mFalseButton;
// Create a Question Array
private Question[] mQuestionBank = new Question[]
{
new Question(R.string.question_australia, true),
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_africa, false),
new Question(R.string.question_americas, true),
new Question(R.string.question_asia, true),
};
// Get the array length
private int maxCurrentIndex = mQuestionBank.length;
// Initialize the Current Index Value
private int mCurrentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate(Bundle) called");
setContentView(R.layout.activity_quiz);
// Instantiate the Text View, Load the initial text, Start an On-Click Listener
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
mQuestionTextView.setText(R.string.text_view);
mQuestionTextView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
incrementQuestion();
}
});
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
checkAnswer(true);
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
checkAnswer(false);
}
});
mPrevButton = (ImageButton) findViewById(R.id.prev_button);
mPrevButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(mCurrentIndex == 0) mCurrentIndex = maxCurrentIndex;
decrementQuestion();
}
});
mNextButton = (ImageButton) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
incrementQuestion();
}
});
}
private void incrementQuestion()
{
mCurrentIndex = (mCurrentIndex +1) % maxCurrentIndex;
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
private void decrementQuestion()
{
mCurrentIndex = (mCurrentIndex - 1) % maxCurrentIndex;
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue)
{
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
int messageResId;
if (userPressedTrue == answerIsTrue)
{
messageResId = R.string.correct_toast;
} else
{
messageResId = R.string.incorrect_toast;
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
}
// LIFE CYCLE METHODS
// @Override
public void OnStart()
{
super.onStart();
Log.d(TAG, “onStart() called”);
}
// @Override
public void OnResume()
{
super.onResume();
Log.d(TAG, “onResume() called”);
}
// @Override
public void OnPause()
{
super.onPause();
Log.d(TAG, “onPause() called”);
}
// @Override
public void OnStop()
{
super.onStop();
Log.d(TAG, “onStop() called”);
}
// @Override
public void OnDestroy()
{
super.onDestroy();
Log.d(TAG, “onDestroy() called”);
}
}