包含api提取的活动意外关闭

如何解决包含api提取的活动意外关闭

我正在开发一个测验应用程序,该应用程序可以从open trivia database获取问题。问题是当意图试图启动活动时,应用程序崩溃。我试过使用凌空抽气和翻新,但都产生相同的问题。谁能帮我吗?

class QuestionActivity : BaseActivity(),View.OnClickListener {

    private lateinit var mQuestionList : ArrayList<Question>
    private lateinit var listofQuestion : ArrayList<Question>

    private var mCurrentPosition : Int = 1
    private var mSelectedPosition : Int = 0
    private var mCorrectAnswers : Int = 0

    private var startTime : Long? = null
    private var endTime : Long? = null
    private var elapsedtime : Float? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_question)

        hidesystemUI()

        showProgressDialog("getting questions from the cloud!")

        fetchQuestions()

        mQuestionList = if(listofQuestion.size > 0){
            listofQuestion
        } else{
            Constants.getQuestionsList()
        }

        setQuestion()

        tv_option_One.setonClickListener(this)
        tv_option_Two.setonClickListener(this)
        tv_option_Three.setonClickListener(this)
        tv_option_Four.setonClickListener(this)
        btn_Submit.setonClickListener(this)
    }

这是我用来获取问题的方法

    private fun fetchQuestions() {
        // Instantiate the RequestQueue.
        val queue = Volley.newRequestQueue(this)
        val url = "https://opentdb.com/api.PHP?amount=10&category=18&difficulty=medium&type=multiple"

        // Request a string response from the provided URL.
        val request = JsonObjectRequest(Request.Method.GET,url,null,{
            try {
                val resultsArray : JSONArray = it.getJSONArray("results")
                for (i in 0 until resultsArray.length()){
                    val questionObject : JSONObject = resultsArray.getJSONObject(i)
                    val incorrectAnswersArray : JSONArray = questionObject.getJSONArray("incorrect_answers")
                    val question = Question(i,questionObject.getString("question"),questionObject.getString("correct_answer"),incorrectAnswersArray.getString(0),incorrectAnswersArray.getString(1),incorrectAnswersArray.getString(2),1)
                    listofQuestion.add(i,question)
                }
                hideProgressDialog()
            }catch (e : JSONException){
                Toast.makeText(this,e.message,Toast.LENGTH_LONG).show()
            }
        },{
                Toast.makeText(this,it.message,Toast.LENGTH_LONG).show()
            })

// Add the request to the RequestQueue.
        queue.add(request)
    }

    private fun setQuestion(){

        val question = mQuestionList[mCurrentPosition-1]

        makeOtherOptionsDefault()

        if(mCurrentPosition == mQuestionList.size){
            btn_Submit.text = resources.getString(R.string.finish)
        }
        else{
            btn_Submit.text = resources.getString(R.string.submit)
        }

        progress_bar.progress = mCurrentPosition
        tv_Question.text = question.question

        startTime = System.currentTimeMillis()

        val progresstext = "$mCurrentPosition / ${progress_bar.max}"

        tv_Progress_value.text = progresstext

        tv_option_One.text = question.optionOne
        tv_option_Two.text = question.optionTwo
        tv_option_Three.text = question.optionThree
        tv_option_Four.text = question.optionFour
    }


    private fun makeOtherOptionsDefault(){
        val options : ArrayList<TextView> = ArrayList()
        options.add(0,tv_option_One)
        options.add(1,tv_option_Two)
        options.add(2,tv_option_Three)
        options.add(3,tv_option_Four)

        for(option in options){
            option.setTextColor(Color.parseColor("#7A8089"))
            option.typeface = Typeface.DEFAULT
            option.background = ContextCompat.getDrawable(this,R.drawable.default_option_border_bg)
        }
    }

    private fun selectedOption(tv : TextView,selectedOptionPosition : Int){
        makeOtherOptionsDefault()
        mSelectedPosition = selectedOptionPosition
        tv.setTextColor(Color.parseColor("#7A8089"))
        tv.setTypeface(tv.typeface,Typeface.BOLD)
        tv.background = ContextCompat.getDrawable(this,R.drawable.selected_option_border_bg)
    }

    private fun answerView(option : Int,drawableView : Int){
        when(option){
            1 ->{
                tv_option_One.background = ContextCompat.getDrawable(this,drawableView)
            }
            2 ->{
                tv_option_Two.background = ContextCompat.getDrawable(this,drawableView)
            }
            3 ->{
                tv_option_Three.background = ContextCompat.getDrawable(this,drawableView)
            }
            4 ->{
                tv_option_Four.background = ContextCompat.getDrawable(this,drawableView)
            }
        }
    }

这是导致崩溃的意图。

    override fun onClick(view: View?) {
        when(view!!.id){
            R.id.tv_option_One ->{
                selectedOption(tv_option_One,1)
            }
            R.id.tv_option_Two ->{
                selectedOption(tv_option_Two,2)
            }
            R.id.tv_option_Three ->{
                selectedOption(tv_option_Three,3)
            }
            R.id.tv_option_Four ->{
                selectedOption(tv_option_Four,4)
            }
            R.id.btn_Submit ->{
                if (mSelectedPosition == 0){
                    mCurrentPosition++

                    if(mCurrentPosition <= mQuestionList.size){
                        setQuestion()
                    }
                    else{
                        val intent = Intent(this,ResultActivity::class.java)
                        intent.putExtra(Constants.CORRECT_ANSWERS,mCorrectAnswers)
                        intent.putExtra(Constants.TOTAL_QUESTION,mQuestionList.size)
                        intent.putExtra(Constants.ELAPSED_TIME,elapsedtime)
                        startActivity(intent)
                        finish()
                    }
                }
                else{
                    val question = mQuestionList[mCurrentPosition-1]
                    if(question.correctOption != mSelectedPosition){
                        answerView(mSelectedPosition,R.drawable.wrong_option_bg)
                    }
                    else{
                        mCorrectAnswers++
                    }
                    answerView(question.correctOption,R.drawable.correct_option_bg)

                    if(mCurrentPosition == mQuestionList.size){
                        btn_Submit.text = resources.getString(R.string.finish)
                        endTime = System.currentTimeMillis()

                        val df = DecimalFormat("#.##")
                        df.roundingMode = RoundingMode.CEILING

                        elapsedtime = df.format(((endTime!! - startTime!!).toFloat() / (1000f*60f))).toFloat()
                    }
                    else{
                        btn_Submit.text = resources.getString(R.string.go_to_next_ques)
                    }

                    mSelectedPosition = 0
                }
            }
        }
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?