如何解决包含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 举报,一经查实,本站将立刻删除。