###前提・実現したいこと
MainActivity.ktでUser.ktとActionViewの定義したクラスを参照したいです。
###発生している問題・エラーメッセージ
unresolved reference
MainActivityで、他のファイルのクラスを参照できません。
###該当のソースコード
参照したいのは、User.ktのUserクラスとActionView.ktのActionViewクラスです。
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 = "Kotlin入門", url = "http://www.example.com/articles/123", user = ★User★(id = "456", name = "たろう", 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) } }
ネットで調べたところ、kotlinの初期設定に問題がある可能性もあるようですので、
gradleも載せます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() }
###補足情報(言語/FW/ツール等のバージョンなど)
AndroidStudio 2.3.1
Kotlin 1.1.2-2
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。