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

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

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

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

Android Studio

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

Kotlin

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

Q&A

解決済

1回答

1754閲覧

「超シンプルなホームアプリ」作り方

nogono

総合スコア2

Android

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

Android Studio

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

Kotlin

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

0グッド

0クリップ

投稿2020/08/03 08:27

前提・実現したいこと

Android Studioを使ってホームアプリを開発中です。言語はKotlinです。
下のサイトの指示通りに作業を進めています。
超シンプルなホームアプリ(ランチャーアプリ)を作る
ところが、初心者の自分には理解できないエラーが発生します。

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

Unresolved referenceが大量に発生します。具体的なエラーエラーメッセージは、下記のソースコードにメモしています。

該当のソースコード

LauncherActivity.kt

package com.example.yorimiti_block1 import androidx.appcompat.app.AppCompatActivity import android.os.Bundle class LauncherActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(layout.activity_main) //Unresolved reference: layout adapter = AppAdapter(layoutInflater, AppInfoList.create(this)) { view, info -> info.launch(this)} //Unresolved reference: adapter//Unresolved reference: AppInfoList recyclerView.adapter = adapter //Unresolved reference: recyclerView//Unresolved reference: adapter recyclerView.layoutManager = LinearLayoutManager(this) //Unresolved reference: recyclerView//Unresolved reference: LinearLayoutManager } }

AppInfoList.kt

package com.example.yorimiti_block1 import android.content.ComponentName import android.content.Context import android.content.Intent import android.content.pm.PackageManager fun create(context: Context): List<AppInfo> { val pm = context.packageManager val intent = Intent(Intent.ACTION_MAIN) .also { it.addCategory(Intent.CATEGORY_LAUNCHER) } return pm.queryIntentActivities(intent, PackageManager.MATCH_ALL) .asSequence() .mapNotNull { it.activityInfo } .filter { it.packageName != context.packageName } .map { AppInfo( it.loadIcon(pm) ?: getDefaultIcon(context), //Unresolved reference: getDefaultIcon it.loadLabel(pm).toString(), ComponentName(it.packageName, it.name) ) } .sortedBy { it.label } .toList() }

AppAdapter.kt

package com.example.yorimiti_block1 import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Adapter import android.widget.ImageView import android.widget.TextView class AppAdapter( private val inflater: LayoutInflater, private val list: List<AppInfo>, private val onClick: (view: View, info: AppInfo) -> Unit ) : Adapter<AppAdapter.AppViewHolder>() { //No type arguments expected for interface Adapter override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): //'onCreateViewHolder' overrides nothing AppViewHolder = AppViewHolder(inflater.inflate(layout.li_application, parent, false)) //Unresolved reference: layout override fun getItemCount(): Int = list.size //'getItemCount' overrides nothing override fun onBindViewHolder(holder: AppViewHolder, position: Int) //'onBindViewHolder' overrides nothing { val info = list[position] holder.itemView.setOnClickListener { onClick(it, info) } //Unresolved reference: itemView //Unresolved reference: it holder.icon.setImageDrawable(info.icon) holder.label.text = info.label holder.packageName.text = info.componentName.packageName } class AppViewHolder(itemView: View) : ViewHolder(itemView) //Unresolved reference: ViewHolder { val icon: ImageView = itemView.findViewById(id.icon) //Unresolved reference: id val label: TextView = itemView.findViewById(id.label) //Unresolved reference: id val packageName: TextView = itemView.findViewById(id.packageName) //Unresolved reference: id } }

###調べたこと
Unresolved referenceはそれぞれの場合で解決の仕方が異なっていて、方法は分かりませんでした。

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

Android Studio 4.0.1

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

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

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

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

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

guest

回答1

0

ベストアンサー

それぞれ次のように修正すれば通るのではないでしょうか。必要な箇所だけ記述しています。

LauncherActivity.kt

kotlin

1import kotlinx.android.synthetic.main.activity_main.* // 追加 2 3 setContentView(R.layout.activity_main) // R.をつける 4 // valをつける 5 val adapter = AppAdapter(layoutInflater, AppInfoList.create(this)) { view, info -> 6 info.launch(this) 7 } 8 9

AppInfoList.kt

kotlin

1object AppInfoList { 2 fun create(context: Context): List<AppInfo> { 3 (修正なし) 4 } 5 6 // 適当なgetDefaultIcon()関数を追加する。下記は一例。 7 private fun getDefaultIcon(context: Context): Drawable { 8 return context.resources.getDrawable(R.mipmap.ic_launcher, null) 9 } 10}

AppAdapter.kt

kotlin

1import androidx.recyclerview.widget.RecyclerView.Adapter // 追加 2import androidx.recyclerview.widget.RecyclerView.ViewHolder // 追加 3 4 5 val icon: ImageView = itemView.findViewById(R.id.icon) // R.をつける 6 val label: TextView = itemView.findViewById(R.id.label) // R.をつける 7 val packageName: TextView = itemView.findViewById(R.id.packageName) // R.をつける

これに加えて、activity_main.xmlも次のようにする必要があるでしょう。

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".LauncherActivity"> 8 9 <androidx.recyclerview.widget.RecyclerView 10 android:id="@+id/recyclerView" 11 android:layout_width="0dp" 12 android:layout_height="0dp" 13 app:layout_constraintBottom_toBottomOf="parent" 14 app:layout_constraintEnd_toEndOf="parent" 15 app:layout_constraintStart_toStartOf="parent" 16 app:layout_constraintTop_toTopOf="parent" /> 17 18</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest.xmlもactivityタグのandroid:nameがLauncherActivityになっているか確認しましょう。

xml

1 <activity android:name=".LauncherActivity">

投稿2020/08/04 02:53

keicha_hrs

総合スコア6766

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

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

nogono

2020/08/06 07:49

お書きいただいた修正をして、サイトの見本通りのものを作ることができました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問