首页 > 解决方案 > 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]

有什么帮助吗?

标签: androidscala

解决方案


目前尚不清楚您如何构建您的 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/


推荐阅读