So I am new to Android Programming and am trying to make my own true false quiz App. However when I try to run the app to see if I am doing this right the emulator shows "Unfortunately, QuizMe has stopped.", where QuizMe is name of the App.
The QuizMeActivity file:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class QuizMeActivity extends ActionBarActivity {
//Variables
//Buttons
private Button mTrueButton;
private Button mFalseButton;
//Image Buttons
private ImageButton mNext;
//Text View
private TextView mQuestionText;
private TextView mAnswerBox;
//Integer, to keep track of location in array
private int mCurrentIndex = 0;
//boolean to hold the answer type
boolean isTrue;
//create an array of TrueFalse to hold each question with answer
private TrueFalse[] mQuestionBankFailed = new TrueFalse[] {
new TrueFalse(R.string.questionOne, R.string.answerOne),
new TrueFalse(R.string.questionTwo, R.string.answerTwo),
new TrueFalse(R.string.questionThree, R.string.answerThree),
new TrueFalse(R.string.questionFour, R.string.answerFour),
new TrueFalse(R.string.questionFive, R.string.answerFive)
};
//create an array of TrueFalse to hold each question with the answer boolean
private TrueFalse[] mQuestionBank = new TrueFalse[] {
new TrueFalse(R.string.questionOne, false),
new TrueFalse(R.string.questionTwo, false),
new TrueFalse(R.string.questionThree, true),
new TrueFalse(R.string.questionFour, true),
new TrueFalse(R.string.questionFive, true)
};
//add methods
//to update the question in screen
private void updateQues(){
int question = mQuestionBank[mCurrentIndex].isQuestion();
mQuestionText.setText(question);
}
//check the answer:
//when user presses true
private void checkAnswerTrue(){
//answer to the question
isTrue = mQuestionBank[mCurrentIndex].isAnswerType();
if(isTrue == true){
mAnswerBox.setText(R.string.C);
}
else{
mAnswerBox.setText(R.string.W);
}
}
//when the user presses false
private void checkAnswerFalse(){
//answer to the question
isTrue = mQuestionBank[mCurrentIndex].isAnswerType();
if(isTrue == false){
mAnswerBox.setText(R.string.C);
}
else{
mAnswerBox.setText(R.string.W);
}
}
//generated methods
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_me);
mQuestionText = (TextView)findViewById(R.id.question_label);
mTrueButton = (Button)findViewById(R.id.buttonT);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswerTrue();
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswerFalse();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz_me, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The TrueFalse class
public class TrueFalse {
//variables
private int mQuestion, mAnswerQuestion;
private boolean mAnswerType;
/*
* Constructor
* sets the variables equal
* to the parameter
*
*/
public TrueFalse(int question, int answer) {
mQuestion = question;
mAnswerQuestion = answer;
}
//overload constructor
public TrueFalse(int question, boolean answer){
mQuestion = question;
mAnswerType = answer;
}
/*
* @return the answerType
*/
public boolean isAnswerType() {
return mAnswerType;
}
/*
* @param answerType the answerType to set
*/
public void setAnswerType(boolean answerType) {
mAnswerType = answerType;
}
/*
* @return the Question
*/
public int isQuestion() {
return mQuestion;
}
/*
* @param Question the mQuestion to set
*/
public void setQuestion(int question) {
this.mQuestion = question;
}
/*
* @return the AnswerQuestion
*/
public int isAnswerQuestion() {
return mAnswerQuestion;
}
/*
* @param AnswerQuestion the mAnswerQuestion to set
*/
public void setAnswerQuestion(int answer) {
this.mAnswerQuestion = answer;
}
}
This is my activity_quiz_me file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mugrai.luvneesh.quizme.QuizMeActivity" >
<!-- android:paddingBottom="@dimen/activity_vertical_margin" -->
<!-- android:paddingLeft="@dimen/activity_horizontal_margin" -->
<!-- android:paddingRight="@dimen/activity_horizontal_margin" -->
<!-- android:paddingTop="@dimen/activity_vertical_margin" -->
<TextView
android:id="@+id/question_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/questionOne"
android:textSize="25sp" />
<Button
android:id="@+id/buttonT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/question_label"
android:layout_marginEnd="44dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="44dp"
android:layout_marginStart="30dp"
android:layout_marginTop="50dp"
android:text="@string/True" />
<Button
android:id="@+id/buttonF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/buttonT"
android:layout_marginEnd="44dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="44dp"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:text="@string/False" />
<TextView
android:id="@+id/answerBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonF"
android:layout_marginEnd="130dp"
android:layout_marginLeft="130dp"
android:layout_marginRight="44dp"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:text="TEMP" />
</RelativeLayout>
The string xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Questions and Answers -->
<string name="app_name">QuizMe</string>
<string name="action_settings">Settings</string>
<string name="questionOne">The average person will shed 10 pounds of skin during their lifetime.</string>
<string name="answerOne">False</string>
<string name="questionTwo">You can lead a cow down starts but not upstairs.</string>
<string name="answerTwo">False</string>
<string name="questionThree">Google was originally called "Backrub."</string>
<string name="answerThree">True</string>
<string name="questionFour">President Calvin coolidge had a pet Racoon.</string>
<string name="answerFour">True</string>
<string name="questionFive">Approximately one quarter of human bones are in the feet.</string>
<string name="answerFive">True</string>
<!-- Buttons -->
<string name="True">True</string>
<string name="False">False</string>
<!-- Right/Wrong -->
<string name="C">Correct</string>
<string name="W">Wrong</string>
</resources>
This is the AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mugrai.luvneesh.quizme"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >
<activity
android:name=".QuizMeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This is the LogCat
10-13 05:45:36.124: E/AndroidRuntime(788): FATAL EXCEPTION: main
10-13 05:45:36.124: E/AndroidRuntime(788): java.lang.RuntimeException: Unable to start activity ComponentInfo{mugrai.luvneesh.quizme/mugrai.luvneesh.quizme.QuizMeActivity}: java.lang.NullPointerException
10-13 05:45:36.124: E/AndroidRuntime(788): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-13 05:45:36.124: E/AndroidRuntime(788): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-13 05:45:36.124: E/AndroidRuntime(788): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-13 05:45:36.124: E/AndroidRuntime(788): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-13 05:45:36.124: E/AndroidRuntime(788): at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 05:45:36.124: E/AndroidRuntime(788): at android.os.Looper.loop(Looper.java:137)
10-13 05:45:36.124: E/AndroidRuntime(788): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-13 05:45:36.124: E/AndroidRuntime(788): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 05:45:36.124: E/AndroidRuntime(788): at java.lang.reflect.Method.invoke(Method.java:511)
10-13 05:45:36.124: E/AndroidRuntime(788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-13 05:45:36.124: E/AndroidRuntime(788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-13 05:45:36.124: E/AndroidRuntime(788): at dalvik.system.NativeStart.main(Native Method)
10-13 05:45:36.124: E/AndroidRuntime(788): Caused by: java.lang.NullPointerException
10-13 05:45:36.124: E/AndroidRuntime(788): at mugrai.luvneesh.quizme.QuizMeActivity.onCreate(QuizMeActivity.java:106)
10-13 05:45:36.124: E/AndroidRuntime(788): at android.app.Activity.performCreate(Activity.java:5104)
10-13 05:45:36.124: E/AndroidRuntime(788): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-13 05:45:36.124: E/AndroidRuntime(788): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-13 05:45:36.124: E/AndroidRuntime(788): ... 11 more
Also this is what the emulator looks like when it shows the message http://gyazo.com/2573198c16c0e357ea960958059214e3
Thank you for any advice and help in advance.