【发布时间】:2021-02-10 22:14:39
【问题描述】:
我是一个老程序员,对 android 和 Kotlin(以及本世纪发明的任何东西顺便说一句)完全陌生
我正在尝试为我的 android 盒子创建一个简单的应用程序,它使用捆绑的遥控器浏览 url 列表,但我真的不知道如何制作这个 url 列表,因为我不知道 Kotlin 如何处理一个数组。
我在网上找到了很多文档,但基本上我完全错过了基础知识。
无论如何,我并不是真的想学习所有内容,只是想通过索引创建一个全球可访问的“频道”列表。 我报告我的代码到现在:
package com.dm.tutorialwebview
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.KeyEvent
import android.view.Menu
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
class MainActivity : AppCompatActivity() {
var webview: WebView? = null
data class Channel(val number: Int, val name:String, val url: String )
val channelsList: MutableList<Channel> = mutableListOf()
var curChannel: Int = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
channelsList.add(1,Channel(1,"channel 1","https://...3"))
channelsList.add(2,Channel(2,"channel 2","https://..."))
channelsList.add(3,Channel(3,"channel 3","https://..."))
channelsList.add(4,Channel(4,"channel 4","https://...u8"))
channelsList.add(5,Channel(5,"channel 5","https://...u8"))
webview = findViewById(R.id.myweb)
webview!!.webViewClient = WebViewClient()
webview!!.settings.javaScriptEnabled = false
webview!!.webChromeClient = WebChromeClient()
webview!!.settings.domStorageEnabled = true
webview!!.settings.builtInZoomControls = false // abilita comandi zoom
webview!!.settings.setSupportZoom(false) // abilita zoom
webview!!.overScrollMode = WebView.OVER_SCROLL_NEVER
webview!!.settings.useWideViewPort = true
webview!!.setInitialScale(1)
curChannel = 1
webview!!.loadUrl(channelsList[curChannel].url)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; questo aggiunge elementi alla barra delle azioni se è presente.
menuInflater.inflate(R.menu.menu_main, menu)
for (i in 0 until menu.size()) menu.getItem(i).isVisible = false
return true
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
return when (keyCode) {
KeyEvent.KEYCODE_PAGE_UP -> {
if (curChannel == 5) return true
curChannel++
webview!!.loadUrl(channelsList[curChannel].url)
true
}
KeyEvent.KEYCODE_PAGE_DOWN -> {
if (curChannel == 1) return true
curChannel--
webview!!.loadUrl(channelsList[curChannel].url)
true
}
else -> super.onKeyUp(keyCode, event)
}
}
}
不幸的是,应用似乎在 Add 方法上崩溃了。
我知道这很简单,但我仍然无法理解它是如何工作的
我需要一些关于修复“channelsList”声明和填充的提示
对不起,如果我在这篇文章中做错了什么,这是第一个。
任何帮助将不胜感激。
谢谢
【问题讨论】:
-
Unfortunately the app seem to crash on Add method数组和列表在 kotlin 中从索引 0 开始,但是您尝试在索引 1 处添加第一个元素,如果您想添加,则根本不需要使用索引列表末尾的项目
标签: android list android-studio kotlin