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

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

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

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

Q&A

0回答

1144閲覧

【kotlin】リサイクラービュー No adapter attached; skipping layout エラーを解決したい

userUser

総合スコア0

Kotlin

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

0グッド

0クリップ

投稿2020/11/20 04:37

編集2020/11/21 09:45

android開発初心者です。
fragmentの中のrecyclerviewで、簡単なアイテムが並ぶだけの画面を作りたいのですが以下のエラーメッセージが表示され、画面にも何も表示されません。
何が原因なのでしょうか。

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

RecyclerView: No adapter attached; skipping layout

該当のソースコード

kotlin

1 2//GuideFragment 3import android.os.Bundle 4import android.view.LayoutInflater 5import android.view.View 6import android.view.ViewGroup 7import androidx.fragment.app.Fragment 8import androidx.lifecycle.ViewModelProvider 9import androidx.recyclerview.widget.LinearLayoutManager 10import androidx.recyclerview.widget.RecyclerView 11import com.example.myapplication.R 12 13 14 15class GuideFragment : Fragment() { 16 17 18 private lateinit var guideViewModel: GuideViewModel 19 private lateinit var recyclerView: RecyclerView 20 private lateinit var guideList: ArrayList<Guide> 21 private lateinit var gAdapter: GuideAdapter 22 23 override fun onCreateView( 24 inflater: LayoutInflater, container: ViewGroup?, 25 savedInstanceState: Bundle? 26 ): View? { 27 28 val manager = LinearLayoutManager(activity) 29 recyclerView.layoutManager = manager 30 31 guideViewModel = 32 ViewModelProvider(this).get(GuideViewModel::class.java) 33 val root = inflater.inflate(R.layout.fragment_more_help_guide, container, false) 34 35 val one = Guide("質問1") 36 val two = Guide("質問2") 37 val three = Guide("質問3") 38 39 guideList = arrayListOf(one, two, three) 40 41 recyclerView = root.findViewById(R.id.guide_recycler_view) 42 43 44 45 gAdapter = GuideAdapter(guideList) 46 recyclerView.adapter = gAdapter 47 48 return root 49 } 50 51}

該当のソースコード

kotlin

1//GuideAdapter 2 3import android.view.LayoutInflater 4import android.view.View 5import android.view.ViewGroup 6import android.widget.TextView 7import androidx.recyclerview.widget.RecyclerView 8import com.example.myapplication.R 9 10 11class GuideAdapter(private val guideList: ArrayList<Guide>): RecyclerView.Adapter<GuideAdapter.ViewHolder>() { 12 13 data class Guide( 14 val question: String 15 ) 16 17 18 class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { 19 val question: TextView = view.findViewById(R.id.question_textView) 20 } 21 22 23 override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder { 24 val view = LayoutInflater.from(viewGroup.context).inflate(R.layout.fragment_more_help_guide_q, viewGroup, false) 25 return ViewHolder(view) 26 } 27 28 29 override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) { 30 val guide = guideList[position] 31 viewHolder.question.text = guide.question 32 33 } 34 35 override fun getItemCount() = guideList.size 36}

該当のソースコード

kotlin

1//FragmentのXML 2<?xml version="1.0" encoding="utf-8"?> 3 4<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent"> 8 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:orientation="vertical"> 13 14 15 <androidx.recyclerview.widget.RecyclerView 16 android:id="@+id/guide_recycler_view" 17 app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" 18 android:layout_width="match_parent" 19 android:layout_height="match_parent" 20 android:layout_marginTop="10dp" 21 /> 22 </LinearLayout> 23 24</FrameLayout>

該当のソースコード

kotlin

1//fragment_more_help_guide_q(アイテムのXML) 2<?xml version="1.0" encoding="utf-8"?> 3<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 xmlns:tools="http://schemas.android.com/tools" 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content"> 8 9 10 11 12 13 <TextView 14 android:id="@+id/question_textView" 15 android:layout_width="match_parent" 16 android:layout_height="28dp" 17 android:layout_marginStart="10dp" 18 android:layout_marginLeft="10dp" 19 android:layout_marginTop="6dp" 20 android:layout_marginEnd="50dp" 21 android:layout_marginRight="50dp" 22 android:gravity="center_vertical" 23 android:text="TextView" 24 app:layout_constraintEnd_toEndOf="parent" 25 app:layout_constraintTop_toTopOf="parent" /> 26 27 28</FrameLayout>

該当のソースコード

kotlin

1//Activity 2import android.os.Bundle 3import com.google.android.material.bottomnavigation.BottomNavigationView 4import androidx.appcompat.app.AppCompatActivity 5import androidx.navigation.findNavController 6import androidx.navigation.ui.setupWithNavController 7 8class TopActivity : AppCompatActivity() { 9 override fun onCreate(savedInstanceState: Bundle?) { 10 super.onCreate(savedInstanceState) 11 setContentView(R.layout.top_activity) 12 13 val navView: BottomNavigationView = findViewById(R.id.nav_view) 14 15 val navController = findNavController(R.id.nav_host_fragment) 16 // Passing each menu ID as a set of Ids because each 17 // menu should be considered as top level destinations. 18 19 navView.setupWithNavController(navController) 20 } 21}

該当のソースコード

xml

1//Activityのxml 2<?xml version="1.0" encoding="utf-8"?> 3<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:app="http://schemas.android.com/apk/res-auto" 5 android:id="@+id/container" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent"> 8 9 <com.google.android.material.bottomnavigation.BottomNavigationView 10 android:id="@+id/nav_view" 11 app:labelVisibilityMode="labeled" 12 android:layout_width="0dp" 13 android:layout_height="wrap_content" 14 android:layout_marginStart="0dp" 15 android:layout_marginEnd="0dp" 16 android:background="?android:attr/windowBackground" 17 app:layout_constraintBottom_toBottomOf="@+id/nav_host_fragment" 18 app:layout_constraintLeft_toLeftOf="parent" 19 app:layout_constraintRight_toRightOf="parent" 20 app:menu="@menu/bottom_nav_menu" /> 21 22 <fragment 23 android:id="@+id/nav_host_fragment" 24 android:name="androidx.navigation.fragment.NavHostFragment" 25 android:layout_width="match_parent" 26 android:layout_height="match_parent" 27 app:defaultNavHost="true" 28 app:layout_constraintBottom_toTopOf="@id/nav_view" 29 app:layout_constraintLeft_toLeftOf="parent" 30 app:layout_constraintRight_toRightOf="parent" 31 app:layout_constraintTop_toTopOf="parent" 32 app:navGraph="@navigation/mobile_navigation" /> 33 34</androidx.constraintlayout.widget.ConstraintLayout>

試したこと

最初Layoutmanagerを忘れていたので追加してみましたが、やはり同じエラーの表示がありました

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

ここにより詳細な情報を記載してください。

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

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

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

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

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

keicha_hrs

2020/11/21 08:01

Fragmentを描画するActivityのコードやレイアウトはどうしているのでしょうか?また、提示されたコードでLinearLayoutManager()を設定している箇所では、まだ変数recyclerViewへの代入が行われていませんから、このまま実行したら必ずエラーで落ちるはずです。
userUser

2020/11/21 09:47

すみません、ではLinearLayoutManager()の箇所についてはどのようにかけばいいのでしょうか。。?
keicha_hrs

2020/11/21 10:00

findViewById()で変数recyclerViewに値を代入した以降であれば問題ないでしょう。ただ、それは質問の本質とは異なりますよね。ご提示のコードで(不要な部分を削った上で)手元でActivityにFragmentを描画してみたところ、恐らくそちらが意図しているであろう結果は得られました。そちらでうまくいっていないのは、その過程に問題があるのではないかと思えます。
keicha_hrs

2020/11/22 08:08

新たに追加していただいた情報通りBottomNavigationViewとNavHostFragmentを用いて、bottom_nav_menu.xmlとmobile_navigation.xmlは適当に作成したプロジェクトで動かしましたが、やはりこちらでは再現できませんでした。今の情報だと私にはこれ以上原因を推測することは難しそうです。こうなると、プロジェクト全体を提供していただいて見てみる以外にないように思いますが、GitHubなどにアップロードすることは可能でしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問