Home>
I want to refer to the classes defined by User.kt and ActionView in MainActivity.kt.
unresolved reference
In MainActivity, you cannot reference classes in other files.
source code:I want to refer to User class of User.kt and ActionView class of ActionView.kt.
MainActivity
package sample.qiitaclient
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.View
import android.view.Menu
import android.view.MenuItem
import sample.qiitaclient.model.Article
class MainActivity: AppCompatActivity () {
override fun onCreate (savedInstanceState: Bundle?) {
super.onCreate (savedInstanceState)
setContentView (R.layout.activity_main)
val toolbar = findViewById (R.id.toolbar) as Toolbar
setSupportActionBar (toolbar)
val fab = findViewById (R.id.fab) as FloatingActionButton
fab.setOnClickListener {view->
Snackbar.make (view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction ("Action", null) .show ()
}
val articleView = ArticleView (applicationContext) unresolved reference
articleView.setArticle (Article (id = "123",
title = "Introduction to Kotlin",
url = "http://www.example.com/articles/123",
user = User (id = "456", name = "taro", profileImageUrl = ""))) unresolved reference
setContentView (articleView)
}
}
java/sampleqiitaclient/model/User.kt
package sample.qiitaclient.model
data class User (val id: String,
val name: String,
val profileImageUrl: String)
java/sampleqiitaclient/view/ActionView.kt
package sample.qiitaclient.view
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import sample.qiitaclient.R
import sample.qiitaclient.model.Article
class ArticleView: FrameLayout {
constructor (context: Context?): super (context)
constructor (context: Context ?,
attrs: AttributeSet?): super (context, attrs)
constructor (context: Context ?,
attrs: AttributeSet ?,
defStyleAttr: Int): super (context, attrs, defStyleAttr)
constructor (context: Context ?,
attrs: AttributeSet ?,
defStyleAttr: Int,
defStyleRes: Int): super (context, attrs, defStyleAttr, defStyleRes)
var profileImageView: ImageView? = null
var titleTextView: TextView? = null
var userNameTextView: TextView? = null
init {
LayoutInflater.from (context) .inflate (R.layout.view_article, this)profileImageView = findViewById (R.id.profile_image_view) as ImageView
titleTextView = findViewById (R.id.title_text_view) as TextView
userNameTextView = findViewById (R.id.user_name_text_view) as TextView
}
fun setArticle (article: Article) {
titleTextView? .text = article.title
userNameTextView? .text = article.user.name
profileImageView? .setBackgroundColor (Color.RED)
}
}
When I checked on the net, there seems to be a problem with the initial setting of kotlin, so
m (_ _) m
build.gradle (Project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.1.2-4'
repositories {
jcenter ()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath "org.jetbrains.kotlin: kotlin-gradle-plugin: $kotlin_version"
// NOTE: Do not place your application dependencies here;they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter ()
}
}
task clean (type: Delete) {
delete rootProject.buildDir
}
build.gradle (Module)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "sample.qiitaclient"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile ('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree (dir: 'libs', include: ['* .jar'])
androidTestCompile ('com.android.support.test.espresso: espresso-core: 2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint: constraint-layout: 1.0.2'
compile 'com.android.support:design:25.3.1'
testCompile 'junit: junit: 4.12'
compile "org.jetbrains.kotlin: kotlin-stdlib-jre7: $kotlin_version"
}
repositories {
mavenCentral ()
}
information:(language/FW/tool version etc.)
AndroidStudio 2.3.1
Kotlin 1.1.2-2
-
Answer # 1
Related articles
- android studio - unresolved reference: cannot resolve activity_main
- gradle - unresolved reference in android studio
- android - [kotlin] unresolved reference: dialogbuttonclicklistener () cannot be resolved
- android studio - unresolved refference in cancelalarmmanager () cannot be resolved
- java - no reference site for android studio sqlite room update
- kotlinxandroid becomes an unresolved reference
- android studio - i would like to know the reason why cannot be converted appears in jsonobject (string) and how to deal with it
- when i try to run the app in android studio, it crashes immediately after launch
- java - android studio real machine debugging error
- java - android studio cannot assign to assign an event to the seek bar
- i want to make an sns application in android studio
- java - i want to change the jdk path used only in android studio terminal
- unable to initialize algolia in android studio
- Android studio listview implements list data display data loop display effect
- android studio - andriod studio project creation wizard
- enabling instant run in android studio has no effect
- how to support all versions of android studio
- sqlite - android studio: how to refresh files in device file explorer
- android studio - i don't understand the meaning of the error
- java - android studio 363, build output is garbled
Related questions
- android studio - i would like to know the reason why cannot be converted appears in jsonobject (string) and how to deal with it
- android - [kotlin] unresolved reference: dialogbuttonclicklistener () cannot be resolved
- java - android studio error
- android - kotin array array class how to use arrayof function (i'm addicted)
- android studio error
- android - i am in trouble because the error at build time cannot be resolved
- java - android calendar library
- android studio - you cannot substitute the one selected by spinner
- android - [kotlin] app is forced to close due to screen transition when list is pressed
- [kotlin] [android studio] i don't know how to use realm in suspend function
You should import. . .
MainActivity