Kotlin中的基本适配器问题

时间:2019-01-27 14:11:54

标签: android listview kotlin android-listview baseadapter

我在我的android项目中使用listview。为此,我创建了BaseAdapter的子级。但是我遇到错误,应用崩溃了。

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    val restaurantListView:ListView? = activity?.findViewById(R.id.restaurant)
    restaurantListView?.adapter = RestaurantAdapter()
    }
}

class RestaurantAdapter:BaseAdapter()
{
override fun getCount(): Int {
return 3
}

override fun getItem(position: Int): Any {
    return Any()
}

override fun getItemId(position: Int): Long {
    return 0
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
    return null
}
}
  

2019-01-27 19:32:50.372 25975-25975 / com.example.foodtaskerkotlin E / Android运行时:致命异常:主要       流程:com.example.foodtaskerkotlin,PID:25975       java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int android.view.View.getImportantForAccessibility()'           在android.widget.AbsListView.obtainView(AbsListView.java:2379)           在android.widget.ListView.makeAndAddView(ListView.java:1970)           在android.widget.ListView.fillDown(ListView.java:704)           在android.widget.ListView.fillFromTop(ListView.java:765)           在android.widget.ListView.layoutChildren(ListView.java:1744)           在android.widget.AbsListView.onLayout(AbsListView.java:2161)           在android.view.View.layout(View.java:17523)           在android.view.ViewGroup.layout(ViewGroup.java:5612)           在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)           在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)           在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)           在android.view.View.layout(View.java:17523)           在android.view.ViewGroup.layout(ViewGroup.java:5612)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在android.view.View.layout(View.java:17523)           在android.view.ViewGroup.layout(ViewGroup.java:5612)           在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)           在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)           在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)           在android.view.View.layout(View.java:17523)           在android.view.ViewGroup.layout(ViewGroup.java:5612)           在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)           在android.view.View.layout(View.java:17523)           在android.view.ViewGroup.layout(ViewGroup.java:5612)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在android.view.View.layout(View.java:17523)           在android.view.ViewGroup.layout(ViewGroup.java:5612)           在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)           在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)           在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)           在android.view.View.layout(View.java:17523)           在android.view.ViewGroup.layout(ViewGroup.java:5612)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在android.view.View.layout(View.java:17523)           在android.view.ViewGroup.layout(ViewGroup.java:5612)           在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)           在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)           在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)           在android.view.View.layout(View.java:17523)           在android.view.ViewGroup.layout(ViewGroup.java:5612)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在com.android.internal.policy.DecorView.onLayout(DecorView.java:724)           在android.view.View.layout(View.java:17523)           在android.view.ViewGroup.layout(ViewGroup.java:5612)           在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)           在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)           在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)           在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6301)           在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:871)           在android.view.Choreographer.doCallbacks(Choreographer.java:683)           在android.view.Choreographer.doFrame(Choreographer.java:619)           在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:857)           在android.os.Handler.handleCallback(Handler.java:751)           在android.os.Handler.dispatchMessage(Handler.java:95)           在android.os.Looper.loop(Looper.java:154)           在android.app.ActivityThread.main(ActivityThread.java:6077)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866)   2019-01-27 19:32:50.372 25975-25975 / com.example.foodtaskerkotlin E / AndroidRuntime:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

0 个答案:

没有答案