最近、kotlinスタートブックを使ってkotliの勉強し始めたのですが、その中のアプリケーションを実際に作成するパートでつまずいています。
ArticleView.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) } }
build.gradle
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 defaultConfig { applicationId "sample.qiitaclient" minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
MainActivity
package sample.qiitaclient.model import android.support.v7.app.AppCompatActivity import android.os.Bundle import sample.qiitaclient.view.ArticleView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val articleView = ArticleView(application) articleView.setArticle(Article(id = "123", title = "Kotlin入門", url = "http://www.example.com/articles/123", user = User(id = "456", name = "たろう", profileImageUrl = ""))) setContentView(articleView) } }
ビルドして実行してみると以下のようなエラーが表示されます
Type mismatch: inferred type is Context? but Context was expected
Type mismatch: inferred type is Context? but Context was expected
Type mismatch: inferred type is Context? but Context was expected
Task :app:prepareLintJar UP-TO-DATE
Task :app:generateDebugSources
Task :app:javaPreCompileDebug
Task :app:compileDebugJavaWithJavac
Task :app:instantRunMainApkResourcesDebug
Task :app:mergeDebugShaders UP-TO-DATE
Task :app:compileDebugShaders UP-TO-DATE
Task :app:generateDebugAssets UP-TO-DATE
Task :app:mergeDebugAssets UP-TO-DATE
Task :app:validateSigningDebug UP-TO-DATE
Task :app:signingConfigWriterDebug UP-TO-DATE
Task :app:processInstantRunDebugResourcesApk UP-TO-DATE
Task :app:checkManifestChangesDebug
Task :app:transformClassesWithExtractJarsForDebug UP-TO-DATE
Task :app:transformClassesWithInstantRunVerifierForDebug
Cannot delete /home/yuya/AndroidStudioProjects/QiitaClient/app/build/intermediates/incremental-verifier/debug/android/support/multidex file
Task :app:transformClassesWithDependencyCheckerForDebug
Task :app:compileDebugNdk NO-SOURCE
Task :app:mergeDebugJniLibFolders UP-TO-DATE
Task :app:transformNativeLibsWithMergeJniLibsForDebug
Task :app:processDebugJavaRes NO-SOURCE
Task :app:transformResourcesWithMergeJavaResForDebug
Task :app:transformNativeLibsAndResourcesWithJavaResourcesVerifierForDebug
Task :app:transformClassesWithInstantRunForDebug
Task :app:transformClassesAndClassesEnhancedWithInstantReloadDexForDebug
Task :app:incrementalDebugTasks
Task :app:preColdswapDebug
Task :app:fastDeployDebugExtractor UP-TO-DATE
Task :app:generateDebugInstantRunAppInfo
Task :app:transformClassesWithDexBuilderForDebug
Task :app:transformDexArchiveWithExternalLibsDexMergerForDebug
Task :app:transformDexArchiveWithDexMergerForDebug
Task :app:transformDexWithInstantRunDependenciesApkForDebug
Task :app:instantRunSplitApkResourcesDebug
Time to process all the split resources PT0.361240505S with 2 slaves
Task :app:transformDexWithInstantRunSlicesApkForDebug
Task :app:packageDebug
Task :app:buildInfoGeneratorDebug
Task :app:compileDebugSources
Task :app:assembleDebug
BUILD SUCCESSFUL in 20s
43 actionable tasks: 25 executed, 18 up-to-date
またArticleView.ktのinit{...}の部分のfindViewByIdのところで
Can be converted to findViewById<TextView>(...)
Inspection info: This inspection reports findViewById calls with type casts which can be converted to findViewById with a type parameter from Android 8.0 (API level 26).
というメッセージが表示されます。
わかりにくくて申し訳ありませんが、よろしくおねがいします。

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2019/03/26 11:49