前提・実現したいこと
かんたんなメモ帳のようなアプリを作っています。
装置の内容:
• 赤いボタンを押したら黄色いリストが出て、もう1度押すと黄色いリストをしまう
• プラスボタンを押すと、黄色いリストが増える
• 黄色いリスト、青いリストともに書き込み可能
発生している問題・エラーメッセージ
このような感じで青いリストは表示できるし、書き込みもできる状態です。
そして、黄色いリストも出てくるようなコードを書いてあるのですが、赤いボタンを押しても黄色いリストが出てこず、アプリがダウンしてしまいます。
アプリがダウンせず、ちゃんと黄色いリストが出てくるようにしたい。
該当のソースコード
Kotlin
1package com.example.expandable_cardview_2 2 3import androidx.appcompat.app.AppCompatActivity 4import android.os.Bundle 5import androidx.recyclerview.widget.LinearLayoutManager 6import kotlinx.android.synthetic.main.activity_main.* 7 8class MainActivity : AppCompatActivity() { 9 10 override fun onCreate(savedInstanceState: Bundle?) { 11 super.onCreate(savedInstanceState) 12 setContentView(R.layout.activity_main) 13 14 val cardView= recycler_cardview 15 val itemList = mutableListOf<ExpandableCardViewAdapter.Item>() 16 17 val item = ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.PARENT) 18 item.children = listOf( 19 ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.CHILD), 20 ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.CHILD), 21 ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.CHILD), 22 ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.CHILD), 23 ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.CHILD)) 24 itemList.add(item) 25 26 ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.PARENT) 27 item.children = listOf( 28 ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.CHILD), 29 ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.CHILD), 30 ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.CHILD), 31 ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.CHILD), 32 ExpandableCardViewAdapter.Item(ExpandableCardViewAdapter.CHILD)) 33 itemList.add(item) 34 35 cardView.layoutManager = LinearLayoutManager(this) 36 cardView.adapter = ExpandableCardViewAdapter(itemList) 37 } 38}
リストを広げる class のコードも載せておきます。ただ、下のコードにはエラーが出てきませんでした。
package com.example.expandable_cardview_2 import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import androidx.recyclerview.widget.RecyclerView import kotlinx.android.synthetic.main.cardview_parent.view.* class ExpandableCardViewAdapter(var items: MutableList<Item>) : RecyclerView.Adapter<RecyclerView.ViewHolder> (){ companion object { const val PARENT = 0 const val CHILD = 1 const val OPEN = 0.0F const val CLOSE = 180.0F } data class Item(val type: Int = 0, var text: String = "Default", var children: List<Item>? = null) inner class ItemHolder(v: View) : RecyclerView.ViewHolder(v) { val toggleImageView:ImageView = v.item_toggle_bottun } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { val inflater = LayoutInflater.from(parent.context) var view: View? = null when (viewType) { PARENT -> view = inflater.inflate(R.layout.cardview_parent, parent, false) CHILD -> view = inflater.inflate(R.layout.cardview_child, parent, false) } return this.ItemHolder(view!!) } override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { val itemHolder = holder as? ItemHolder val item = items[position] itemHolder?.let { it -> it.toggleImageView.let { it1 -> it1.setImageResource(R.drawable.toggle) it1.rotation = if (item.children == null) OPEN else CLOSE it1.setOnClickListener {view -> val start: Int = items.indexOf(item) + 1 if (item.children == null) { var count = 0 var nextHeader = items.indexOf(items.find { it2 -> (count++ >= start) && (it2.type == item.type) }) if (nextHeader == -1) nextHeader = items.size item.children = items.slice(start until nextHeader) val end = item.children!!.size if (end > 0) items.removeAll(item.children!!) view.animate().rotation(CLOSE).start() notifyItemRangeRemoved(start, end) } else { item.children?.let { items.addAll(start, it) view.animate().rotation(OPEN).start() notifyItemRangeInserted(start, it.size) item.children = null } } } } } } override fun getItemCount(): Int = items.size override fun getItemViewType(position: Int): Int = items[position].type }
試したこと
クラッシュログを載せておきます。
2020-03-26 18:51:53.846 5070-5070/com.example.expandable_cardview_2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.expandable_cardview_2, PID: 5070 java.lang.IllegalStateException: v.item_toggle_bottun must not be null at com.example.expandable_cardview_2.ExpandableCardViewAdapter$ItemHolder.<init>(ExpandableCardViewAdapter.kt:24) at com.example.expandable_cardview_2.ExpandableCardViewAdapter.onCreateViewHolder(ExpandableCardViewAdapter.kt:36) at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078) at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235) at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118) at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114) at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303) at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627) at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587) at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665) at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134) at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851) at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) at android.widget.LinearLayout.onLayout(LinearLayout.java:1494) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:724) at android.view.View.layout(View.java:17523) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871) at android.view.Choreographer.doCallbacks(Choreographer.java:683) at android.view.Choreographer.doFrame(Choreographer.java:619) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
補足情報(FW/ツールのバージョンなど)
Kotlin version:1.3.71 (最新バージョンです)
Android Studio を使っています。
回答2件
あなたの回答
tips
プレビュー