ExpandableList的活动无法打开

时间:2018-03-06 09:44:24

标签: android kotlin expandablelistview android-adapter

因此,在完成无数教程之后,我以“正确”的方式实现了ExpandableList:Adapter然后在主活动上调用它。

我不断遇到nullnon-null问题。当我尝试添加或删除它们时,有些问题说我需要/不需要空值。

许多安全通话和!!都在那里,因为Android Studio'对此大声喊叫。

这是我的适配器

class ExpandableListAdapter(
        val context: Context,
        val listDataHeader: List<String>,
        val listHashMap: HashMap<String, List<String>>
) : BaseExpandableListAdapter() {


override fun getChild(groupPosition: Int, childPosition: Int): Any {
    return listHashMap.get(listDataHeader.get(groupPosition))!!.get(childPosition); // i = Group Item , i1 = ChildIte
}

override fun getChildId(groupPosition: Int, childPosition: Int): Long {
    return childPosition.toLong()
}

override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, view: View, parent: ViewGroup?): View {
    val childText = getChild(groupPosition, childPosition) as String
    if (view == null) {
        val inflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        inflater.inflate(R.layout.list_item, null)
    }

    val txtListChild = view.findViewById(R.id.lblListItem) as TextView
    txtListChild.text = childText
    return view    }


override fun getChildrenCount(groupPosition: Int): Int {
    return listHashMap[listDataHeader[groupPosition]]!!.size
}

override fun getGroup(groupPosition: Int): Any {
    return listDataHeader[groupPosition]
}

override fun getGroupCount(): Int {
    return listDataHeader.size
}

override fun getGroupId(groupPosition: Int): Long {
    return groupPosition.toLong()
}

override fun getGroupView(groupPosition: Int, isExpanded: Boolean, view: View?, parent: ViewGroup?): View? {
    var headerTitle = getGroup(groupPosition!!) as String
    if (view == null) {
        var inflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        inflater.inflate(R.layout.list_group, null)
    }
    val lblListHeader = view?.findViewById(R.id.lblListHeader) as TextView?
    lblListHeader?.setTypeface(null, Typeface.BOLD)
    lblListHeader?.text = headerTitle
    return view
}

override fun hasStableIds(): Boolean {
    return false
}

override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
    return true
}
}

这是我的主要活动

class GirlsActivity : AppCompatActivity() {

private var db = FirebaseFirestore.getInstance()

lateinit var listView: ExpandableListView
lateinit var listAdapter: ExpandableListAdapter
lateinit var listDataHeader: MutableList<String>
lateinit var listHash: HashMap<String, List<String>>


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_girls)

    listView = girlsWodsList
    initData()
    listAdapter = ExpandableListAdapter(this, listDataHeader, listHash)
    listView?.setAdapter(listAdapter)

}
private fun initData() {
    listDataHeader = ArrayList()
    listHash = HashMap()

    listDataHeader?.add("EDMTDev")
    listDataHeader?.add("Android")
    listDataHeader?.add("Xamarin")
    listDataHeader?.add("UWP")

    val edmtDev = ArrayList<String>()
    edmtDev.add("This is Expandable ListView")

    val androidStudio = ArrayList<String>()
    androidStudio.add("Expandable ListView")
    androidStudio.add("Google Map")
    androidStudio.add("Chat Application")
    androidStudio.add("Firebase ")

    val xamarin = ArrayList<String>()
    xamarin.add("Xamarin Expandable ListView")
    xamarin.add("Xamarin Google Map")
    xamarin.add("Xamarin Chat Application")
    xamarin.add("Xamarin Firebase ")

    val uwp = ArrayList<String>()
    uwp.add("UWP Expandable ListView")
    uwp.add("UWP Google Map")
    uwp.add("UWP Chat Application")
    uwp.add("UWP Firebase ")

    listHash!![listDataHeader!!.get(0)] = edmtDev
    listHash!![listDataHeader!!.get(1)] = androidStudio
    listHash!![listDataHeader!!.get(2)] = xamarin
    listHash!![listDataHeader!!.get(3)] = uwp
}
}

有什么想法吗?

由于

logcat的

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.reecreate.woderator2, PID: 9201 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference                                                                          
   at android.widget.AbsListView.obtainView(AbsListView.java:2380)                                                                          
   at android.widget.ListView.measureHeightOfChildren(ListView.java:1326)                                                                          
   at android.widget.ListView.onMeasure(ListView.java:1233)                                                                          
   at android.view.View.measure(View.java:19857)                                                                          
   at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1054)                                                                          
   at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1111)                                                                          
   at android.view.View.measure(View.java:19857)                                                                          
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)                                                                          
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                          
   at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)                                                                          
   at android.view.View.measure(View.java:19857)                                                                          
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)                                                                          
   at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)                                                                          
   at android.view.View.measure(View.java:19857)                                                                          
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)                                                                          
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                          
   at android.view.View.measure(View.java:19857)                                                                          
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)                                                                          
   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)                                                                          
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)                                                                          
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)                                                                          
   at android.view.View.measure(View.java:19857)                                                                          
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)                                                                          
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                          
   at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)                                                                          
   at android.view.View.measure(View.java:19857)                                                                          
   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)                                                                          
   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)                                                                          
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)                                                                          
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)                                                                          
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)                                                                          
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)                                                                          
   at android.view.Choreographer.doCallbacks(Choreographer.java:686)                                                                          
   at android.view.Choreographer.doFrame(Choreographer.java:621)                                                                          
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)                                                                          
   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:6119)                                                                          
   at java.lang.reflect.Method.invoke(Native Method)                                                                          
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                          
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 个答案:

答案 0 :(得分:0)

我不知道ExpandableListAdapter有什么问题,但我知道,使用Epoxy会更好 - https://github.com/airbnb/epoxy