質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

9884閲覧

kotlinによるアプリケーション開発のエラー(android studio)

oxyu8

総合スコア23

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2019/03/26 05:49

最近、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).

というメッセージが表示されます。

わかりにくくて申し訳ありませんが、よろしくおねがいします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

表示されたメッセージをお読みになりましたでしょうか.

context は null 不可なので Context? では無く Context として定義すべきと思います.

constructor(context: Context?) : super(context) ↓ constructor(context: Context) : super(context)

findViewById は compileSdkVersion 26 以上では as XXXX では無く <XXXX> と書べきと思います.

profileImageView = findViewById(R.id.profile_image_view) as ImageView ↓ profileImageView = findViewById<ImageView>(R.id.profile_image_view)

投稿2019/03/26 10:06

jimbe

総合スコア12648

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

oxyu8

2019/03/26 11:49

無事に解決しました。 本当にありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問