First of all, I am a beginner at android. I am actually trying to build a quiz app. but I am getting stuck with an error that is unexpected.when I clickNext
button, I found an error. I was searching for the same question in StackOverflow but I could not find an expected solution. here is my code:-
MainActivity.java
package com.example.hellowandroid;
import androidx.appcompat.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;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button falsebutton;
private Button truebutton;
private TextView questionTextView;
private ImageButton nextButton;
private int currentQuestionIndex = 0;
private Question[] questionBank = new Question[]
{ new Question(R.string.question_amendments, false),
new Question(R.string.question_constitution, true),
new Question(R.string.question_declaration, true),
new Question(R.string.question_independence_rights, true),
new Question(R.string.question_religion, true),
new Question(R.string.question_government, false),
new Question(R.string.question_government_feds, false),
new Question(R.string.question_government_senators, true),
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
falsebutton = findViewById(R.id.false_button);
truebutton = findViewById(R.id.true_button);
questionTextView = findViewById(R.id.answer_text_view);
nextButton = findViewById (R.id.next_button);
falsebutton.setOnClickListener(this);
truebutton.setOnClickListener(this);
nextButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.false_button:
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_SHORT).show();
break;
case R.id.true_button:
Toast.makeText(MainActivity.this,"true",Toast.LENGTH_SHORT).show();
break;
case R.id.next_button:
//go to next question
currentQuestionIndex = (currentQuestionIndex 1) % questionBank.length;
Log.d("Current Index","onClick" currentQuestionIndex);
questionTextView.setText((questionBank[currentQuestionIndex].getAnswerResId()));
}
}
}
maybe this line questionTextView.setText((questionBank[currentQuestionIndex].getAnswerResId()));
has a problem, I am not sure.
Question.java
package com.example.hellowandroid;
public class Question {
private int answerResId;
private boolean answerTrue;
public Question (int answerResId,boolean answerTrue)
{
answerResId=this.answerResId;
answerTrue=this.answerTrue;
}
public int getAnswerResId() {
return answerResId;
}
public void setAnswerResId(int answerResId) {
this.answerResId = answerResId;
}
public boolean isAnswerTrue() {
return answerTrue;
}
public void setAnswerTrue(boolean answerTrue) {
this.answerTrue = answerTrue;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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/design_default_color_primary_dark"
tools:context=".MainActivity">
<ImageView
android:src="@android:drawable/ic_dialog_dialer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@ id/answer_text_view"
android:text="@string/question_declaration"
android:padding="10dp"
android:textColor="@android:color/background_light"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="64dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@ id/true_button"
android:text="@string/true_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@ id/false_button"
android:text="@string/false_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<ImageButton
android:contentDescription="@string/next_text"
android:id="@ id/next_button"
android:src="@android:drawable/ic_media_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
strings.xml
<resources>
<string name="app_name">HellowAndroid</string>
<string name="true_text">true</string>
<string name="false_text">false</string>
<string name="question_declaration">General Knowledge Of US Students.</string>
<string name="correct_answer">That\'s correct</string>
<string name="wrong_answer">That\'s incorrect</string>
<string name="question_constitution">The Supreme law of the land is the Constitution.</string>
<string name="question_amendments">The (U.S.) Constitution has 26 Amendments.</string>
<string name="question_independence_rights">The two rights in the Declaration of Independence are:
\n \t <b>life</b> \n \t <b>pursuit of happiness</b>.</string>
<string name="question_religion">Freedom of religion means:
\n \t <b>You can practice any religion, or not practice a religion</b>.</string>
<string name="question_government">Journalists is one branch or part of the government.</string>
<string name="question_government_feds">Congress does not make federal laws.</string>
<string name="question_government_senators">There are one hundred (100) U.S. Senators.</string>
<string name="next_text">next</string>
</resources>
Here is the error:-
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hellowandroid, PID: 27079
android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:1184)
at android.widget.TextView.setText(TextView.java:5218)
at com.example.hellowandroid.MainActivity.onClick(MainActivity.java:81)
at android.view.View.performClick(View.java:6312)
at android.view.View$PerformClick.run(View.java:23985)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
also i tried questionTextView.setText("" questionBank[currentQuestionIndex]. getAnswerResId());
but same error. it's main is mainly happend when i click next button.how i resolve this issue.
CodePudding user response:
You have set these variables the wrong way.
public Question (int answerResId, boolean answerTrue)
{
answerResId = this.answerResId;
answerTrue = this.answerTrue;
}
It should have been like this:
public Question (int answerResId, boolean answerTrue)
{
this.answerResId = answerResId;
this.answerTrue = answerTrue;
}
CodePudding user response:
The value of currentQuestionIndex
must be smaller than the value of questionBank.size()
:
if (currentQuestionIndex >= questionBank.size()) {
throw new IndexOutOfBoundsException(
"no item exists at index " currentQuestionIndex
);
}
And I really do not understand the purpose of this %
modulo operation here:
currentQuestionIndex = (currentQuestionIndex 1) % questionBank.length;
The common way of writing that might rather be:
currentQuestionIndex ;