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

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

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

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

Android Studio

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

Kotlin

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

Android Emulator

Android EmulatorはアンドロイドのOSで起動しているアンドロイドのデバイスの機能をシミュレートするソフトウェアです。Emulatorは開発者に複数の違う設定を持ったデバイスを必要とすることなくアプリケーションを開発しテストすることが可能になります。

Q&A

解決済

1回答

2975閲覧

unresolved reference text とエラーが出ています!

f25gm

総合スコア5

Android

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

Android Studio

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

Kotlin

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

Android Emulator

Android EmulatorはアンドロイドのOSで起動しているアンドロイドのデバイスの機能をシミュレートするソフトウェアです。Emulatorは開発者に複数の違う設定を持ったデバイスを必要とすることなくアプリケーションを開発しテストすることが可能になります。

0グッド

0クリップ

投稿2021/08/04 05:05

編集2021/08/04 05:41

前提・実現したいこと

本日kotlinを勉強し始めた初心者です。
Android studio で
ボタンを押したらテキストが変わるというものを作成しているのですが、
エラーが出てしまいます。

発生している問題・エラーメッセージ

unresolved reference text

該当のソースコード

package com.example.myfirstapp import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } fun changeTextView(view : View){ messageTextView.text = "hello threre" } }
plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-android-extensions' } android { compileSdk 31 defaultConfig { applicationId "com.example.myfirstapp" minSdk 21 targetSdk 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } } dependencies { implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myfirstapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyFirstApp"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/messageTextView" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="68dp" android:layout_height="35dp" android:text="@string/message_text" android:textSize="36sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/changeButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:onClick="changeTextView" android:text="@string/button_text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" app:layout_constraintVertical_bias="0.24" /> </androidx.constraintlayout.widget.ConstraintLayout>

試したこと

配置されているものがtextViewではないのではないかとの
他の質問者に対する回答を見つけましたが
textViewで配置されていました。

補足情報(FW/ツールのバージョンなど)

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

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

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

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

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

f25gm

2021/08/04 05:28

機能を教えて下さり、ありがとうございます。 コードを上げなおしました。 activity_main.xmlはアプリの画面?が表示されていると思うのですが、 どこを確認したらよいでしょうか? 無知で申し訳ありません。
hoshi-takanori

2021/08/04 05:33

activity_main.xml を開くと、ウィンドウの右上に Code, Split, Design と出てると思いますが、Code を押すとテキスト表示になるので、その内容を貼ってください。
f25gm

2021/08/04 05:41

ありがとうございます。 貼り付けました!
guest

回答1

0

ベストアンサー

ConstraintLayout の中に TextView と Button がありますが、android:id="@+id/messageTextView" が TextView ではなく、ConstraintLayout に書いてあるため、Kotlin コードの messageTextView.text が ConstraintLayout を指してしまい、ConstraintLayout には text プロパティがないというエラーになってます。

おそらく ConstraintLayout には id は不要だと思いますので、ConstraintLayout の id を消して、TextView の id を messageTextView にすれば良いと思います。方法は、xml コードを直接編集することもできますし、GUI でやる場合は、左下の Component Tree で ConstraintLayout を選んで、右上の id を消すと良いでしょう。

こうなった原因ですが、真ん中のプレビュー領域で、TextView や Button ではない白い部分を押すと ConstraintLayout が選ばれますが、たぶんその状態で TextView が選択されてると勘違いして id を設定してしまったのでしょう。(自分もたまにやります…。)

イメージ説明

投稿2021/08/04 06:03

hoshi-takanori

総合スコア7895

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

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

f25gm

2021/08/04 06:51

ありがとうございます。 無事治りました!何から何まで本当にありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問