android - Scala 与 Android
问题描述
我正在尝试将 scala 与 android 一起使用,我的应用程序仅包含一个按钮,因为它单击它在浏览器中打开了一个链接我已经按照此链接进行了更改,只是我将 java 代码转换为 scala ?
res.layout.main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button - Go to mkyong.com" />
</LinearLayout>
MyAndroidAppActivity.scala
:
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.view.View
import android.view.View.OnClickListener
class MyAndroidAppActivity extends Activity {
var button: Button = null
override def onCreate(savedInstanceState: Bundle): Unit = {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
addListenerOnButton()
}
def addListenerOnButton(): Unit = {
button = findViewById(R.id.button1).asInstanceOf[Button]
button.setOnClickListener(new View.OnClickListener() {
override def onClick(arg0: View): Unit = {
val browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.mkyong.com"))
startActivity(browserIntent)
}
})
}
}
我收到了这个错误:
Error:(16, 20) not found: value R
setContentView(R.layout.main)
Error:(21, 27) not found: value R
button = findViewById(R.id.button1).asInstanceOf[Button]
有什么帮助吗?
解决方案
目前尚不清楚您如何构建您的 Android 应用程序。可能与 Gradle 一起使用。Gradle 构建是否支持 Scala?
尝试在 Android 中使用 Scala 的库之一
https://scala-android.org/ https://github.com/scala-android/sbt-android https://gitter.im/scala-android/sbt-android
https://github.com/pocorall/scaloid http://blog.scaloid.org/
https://github.com/47deg/macroid http://47deg.github.io/macroid/
推荐阅读
- amazon-web-services - AWS CodeDeploy 未在 Lightsail 基本设置 SSL 上部署
- c# - 停止等待更改线程 C# 的请求
- sql - 按定义的条件从表中选择列的次数
- laravel - 将多张图片保存到 Laravel Livewire
- javascript - 在键值对匹配的嵌套对象中求和值
- haskell - Haskell - 将整数列表映射到它们的平方根
- javascript - 从word文档粘贴时,输入字段单引号变成正方形
- excel - VBA:如何检查特定单元格是否包含图像,然后删除图像(如果存在)
- python - 如何在时间序列图的 xticks 上显示自定义间隔?
- python-3.x - 在 Python 中将两个 shapefile 与 geopandas 重叠