0

I has created Quiz App using Android Studio with SQL Database Code but when start to run the App this message is comin and closed the app. please any way to solve this problem.

note: all the code using it can see on this link (https://codinginflow.com/code-examples/android/quiz-app-with-sqlite/part-10)

package com.example.user.quizapp;


import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;

public class QuizActivity extends AppCompatActivity {
    public static final String EXTRA_SCORE = "extraScore";
    private static final long COUNTDOWN_IN_MILLIS = 30000;

    private static final String KEY_SCORE = "keyScore";
    private static final String KEY_Questions_COUNT = "keyQuestionsCount";
    private static final String KEY_MILLIS_LEFT = "keyMillisLeft";
    private static final String KEY_ANSWERED = "keyAnswered";
    private static final String KEY_Questions_LIST = "keyQuestionsList";

    private TextView textViewQuestions;
    private TextView textViewScore;
    private TextView textViewQuestionsCount;
    private TextView textViewDifficulty;
    private TextView textViewCountDown;
    private RadioGroup rbGroup;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private Button buttonConfirmNext;

    private ColorStateList textColorDefaultRb;
    private ColorStateList textColorDefaultCd;

    private CountDownTimer countDownTimer;
    private long timeLeftInMillis;

    private ArrayList<Questions> QuestionsList;
    private int QuestionsCounter;
    private int QuestionsCountTotal;
    private Questions currentQuestions;

    private int score;
    private boolean answered;

    private long backPressedTime;

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

        textViewQuestions = findViewById(R.id.qs_view);
        textViewScore = findViewById(R.id.score_view);
        textViewQuestionsCount = findViewById(R.id.qs_count);
        textViewDifficulty = findViewById(R.id.text_view_difficulty);
        textViewCountDown = findViewById(R.id.qz_timer);
        rbGroup = findViewById(R.id.rad_group);
        rb1 = findViewById(R.id.rad_bttn1);
        rb2 = findViewById(R.id.rad_bttn2);
        rb3 = findViewById(R.id.rad_bttn3);
        buttonConfirmNext = findViewById(R.id.btn_confirm);

        textColorDefaultRb = rb1.getTextColors();
        textColorDefaultCd = textViewCountDown.getTextColors();

        Intent intent = getIntent();
        String difficulty = intent.getStringExtra(MainActivity.EXTRA_DIFFICULTY);

        textViewDifficulty.setText("Difficulty: " + difficulty);

        if (savedInstanceState == null) {
            QuizHelper dbHelper = new QuizHelper(this);
            QuestionsList = dbHelper.getQuestions(difficulty);
            QuestionsCountTotal = QuestionsList.size();
            Collections.shuffle(QuestionsList);

            showNextQuestions();
        } else {
            QuestionsList = savedInstanceState.getParcelableArrayList(KEY_Questions_LIST);
            QuestionsCountTotal = QuestionsList.size();
            QuestionsCounter = savedInstanceState.getInt(KEY_Questions_COUNT);
            currentQuestions = QuestionsList.get(QuestionsCounter - 1);
            score = savedInstanceState.getInt(KEY_SCORE);
            timeLeftInMillis = savedInstanceState.getLong(KEY_MILLIS_LEFT);
            answered = savedInstanceState.getBoolean(KEY_ANSWERED);

            if (!answered) {
                startCountDown();
            } else {
                updateCountDownText();
                showSolution();
            }
        }

        buttonConfirmNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!answered) {
                    if (rb1.isChecked() || rb2.isChecked() || rb3.isChecked()) {
                        checkAnswer();
                    } else {
                        Toast.makeText(QuizActivity.this, "Please select an answer", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    showNextQuestions();
                }
            }
        });
    }

    private void showNextQuestions() {
        rb1.setTextColor(textColorDefaultRb);
        rb2.setTextColor(textColorDefaultRb);
        rb3.setTextColor(textColorDefaultRb);
        rbGroup.clearCheck();

        if (QuestionsCounter < QuestionsCountTotal) {
            currentQuestions = QuestionsList.get(QuestionsCounter);

            textViewQuestions.setText(currentQuestions.getQuestion());
            rb1.setText(currentQuestions.getOption1());
            rb2.setText(currentQuestions.getOption2());
            rb3.setText(currentQuestions.getOption3());

            QuestionsCounter++;
            textViewQuestionsCount.setText("Questions: " + QuestionsCounter + "/" + QuestionsCountTotal);
            answered = false;
            buttonConfirmNext.setText("Confirm");

            timeLeftInMillis = COUNTDOWN_IN_MILLIS;
            startCountDown();
        } else {
            finishQuiz();
        }
    }

    private void startCountDown() {
        countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeLeftInMillis = millisUntilFinished;
                updateCountDownText();
            }

            @Override
            public void onFinish() {
                timeLeftInMillis = 0;
                updateCountDownText();
                checkAnswer();
            }
        }.start();
    }

    private void updateCountDownText() {
        int minutes = (int) (timeLeftInMillis / 1000) / 60;
        int seconds = (int) (timeLeftInMillis / 1000) % 60;

        String timeFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);

        textViewCountDown.setText(timeFormatted);

        if (timeLeftInMillis < 10000) {
            textViewCountDown.setTextColor(Color.RED);
        } else {
            textViewCountDown.setTextColor(textColorDefaultCd);
        }
    }

    private void checkAnswer() {
        answered = true;

        countDownTimer.cancel();

        RadioButton rbSelected = findViewById(rbGroup.getCheckedRadioButtonId());
        int answerNr = rbGroup.indexOfChild(rbSelected) + 1;

        if (answerNr == currentQuestions.getAnswerNr()) {
            score++;
            textViewScore.setText("Score: " + score);
        }

        showSolution();
    }

    private void showSolution() {
        rb1.setTextColor(Color.RED);
        rb2.setTextColor(Color.RED);
        rb3.setTextColor(Color.RED);

        switch (currentQuestions.getAnswerNr()) {
            case 1:
                rb1.setTextColor(Color.GREEN);
                textViewQuestions.setText("Answer 1 is correct");
                break;
            case 2:
                rb2.setTextColor(Color.GREEN);
                textViewQuestions.setText("Answer 2 is correct");
                break;
            case 3:
                rb3.setTextColor(Color.GREEN);
                textViewQuestions.setText("Answer 3 is correct");
                break;
        }

        if (QuestionsCounter < QuestionsCountTotal) {
            buttonConfirmNext.setText("Next");
        } else {
            buttonConfirmNext.setText("Finish");
        }
    }

    private void finishQuiz() {
        Intent resultIntent = new Intent();
        resultIntent.putExtra(EXTRA_SCORE, score);
        setResult(RESULT_OK, resultIntent);
        finish();
    }

    @Override
    public void onBackPressed() {
        if (backPressedTime + 2000 > System.currentTimeMillis()) {
            finishQuiz();
        } else {
            Toast.makeText(this, "Press back again to finish", Toast.LENGTH_SHORT).show();
        }

        backPressedTime = System.currentTimeMillis();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_SCORE, score);
        outState.putInt(KEY_Questions_COUNT, QuestionsCounter);
        outState.putLong(KEY_MILLIS_LEFT, timeLeftInMilAlis);
        outState.putBoolean(KEY_ANSWERED, answered);
        outState.putParcelableArrayList(KEY_Questions_LIST, QuestionsList);
    }
}

here is the error log:

E/SQLiteLog: (1) near "1": syntax error/com.example.user.quizapp /AndroidRuntime: Shutting down VM/com.example.user.quizapp E/AndroidRuntime: ATALEXCEPTION: main Process: com.example.user.quizapp, PID: 2563 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.quizapp/com.example.user.quizapp.QuizActivity}: android.database.sqlite.SQLiteException: near "1": syntax error (code 1): , while compiling: CREATE TABLE QUIZ_QUESTION ( _id INTEGER PRIMARY KEY AUTOINCREMENT, QUESTIONS TEXT, OPTION 1 TEXT, OPTION 2 TEXT, OPTION 3 TEXT, ANSWER_NR INTEGER, difficulty TEXT)

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • 1
    Look at [the stack trace](http://stackoverflow.com/questions/23353173) to determine the cause of the crash. – Mike M. Apr 03 '18 at 03:20
  • Please post stacktrace – Pavan Apr 03 '18 at 03:56
  • After i follow the logcat I find more details error but I cant determine the error exactly its shown as bellow – user8886221 Apr 03 '18 at 03:58
  • E/SQLiteLog: (1) near "1": syntax error/com.example.user.quizapp /AndroidRuntime: Shutting down VM/com.example.user.quizapp E/AndroidRuntime: ATALEXCEPTION: main Process: com.example.user.quizapp, PID: 2563 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.quizapp/com.example.user.quizapp.QuizActivity}: android.database.sqlite.SQLiteException: near "1": syntax error (code 1): , while compiling: CREATE TABLE QUIZ_QUESTION ( _id INTEGER PRIMARY KEY AUTOINCREMENT, QUESTIONS TEXT, OPTION 1 TEXT, OPTION 2 TEXT, OPTION 3 TEXT, ANSWER_NR INTEGER, difficulty TEXT) – user8886221 Apr 03 '18 at 04:08
  • 1
    Some of your column names have a space in them; e.g., `OPTION 1`, `OPTION 2`, etc. Either [quote them](https://stackoverflow.com/a/14031754) everywhere you use them, or, better yet, just change them to something without the space; e.g. `OPTION1`. – Mike M. Apr 03 '18 at 05:25
  • pls post you db handler class where you are creating table – Rajesh Apr 03 '18 at 06:53

0 Answers0