viewGroup在适配器Recyclerview上返回null

时间:2017-11-23 03:06:25

标签: android android-fragments kotlin recycler-adapter android-databinding

我尝试在片段中添加带有Recyclerview的列表。但是当适配器试图给布局充气时,我的适配器出错了。我发现viewGroup为null。我上次成功,但我在活动中使用Recyclerview而不是碎片。我不知道我的错误在哪里。这是我的错误信息。

event = ["{ key:1,label:"R. Bulan"}","{ key:2,label:"R. Bintang"}","{ key:3,label:"R. Akasia"}","{ key:4,label:"R. Guest Room"}"]

这里是我的片段

event =[
    { key:1,label:"R. Bulan"},
    { key:2,label:"R. Bintang"},
    { key:3,label:"R. Akasia"},
    { key:4,label:"R. Guest Room"}
];

这是我的适配器。

android.view.InflateException: Binary XML file line #0: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:543)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
                                                                        at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:116)
                                                                        at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:88)
                                                                        at com.unicorn.muf.mockup.feature.tracking.adapter.TrackingAdapter.onCreateViewHolder(TrackingAdapter.kt:21)
                                                                        at com.unicorn.muf.mockup.feature.tracking.adapter.TrackingAdapter.onCreateViewHolder(TrackingAdapter.kt:15)
                                                                        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493)
                                                                        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)
                                                                        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
                                                                        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
                                                                        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
                                                                        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
                                                                        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
                                                                        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
                                                                        at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
                                                                        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
                                                                        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
                                                                        at android.view.View.layout(View.java:16952)

我在onCreateviewHolder中收到错误。

class TrackingFragment : Fragment(), TrackingAdapter.onItemClick {

private val TAG = "TrackingFragment"
private lateinit var binding : FragmentTrackingBinding
private var list = ArrayList<TrackingModel>()
private var adapter : TrackingAdapter? = null

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    binding = DataBindingUtil.inflate(inflater,R.layout.fragment_tracking,container,false)
    binding.vRecyclerview.layoutManager = LinearLayoutManager(context)
    binding.vRecyclerview.itemAnimator = DefaultItemAnimator()
    adapter = TrackingAdapter(list,context)
    adapter!!.setOnClick(this)
    binding.vRecyclerview.adapter = adapter

    initdummydata()
    return binding.root
}

override fun onClickItem(model: TrackingModel) {
    val gson = Gson()
    val intent = Intent(context,DetailTrackingActivity::class.java)
    intent.putExtra(Constants.KEY_GSON, gson.toJson(model))
    startActivity(intent)
}

private fun initdummydata(){
    Log.d(TAG, "initData")
    list.add(TrackingModel("WO",5000000,"Karyawan","Parji", "xxxxxasdax", "BA 1234 BA", "Truck", "Asus", "Jakarta", "A4X", "1234", "12334"))
    list.add(TrackingModel("WO",40000,"Karyawan","Parjo", "xxxxxasdax", "BA 1234 BA", "Truck", "Asus", "Jakarta", "A4X", "1234", "12334"))
    list.add(TrackingModel("WO",5000000,"Karyawan","Parje", "xxxxxasdax", "BA 1234 BA", "Truck", "Asus", "Jakarta", "A4X", "1234", "12334"))
    list.add(TrackingModel("WO",1230000,"Karyawan","Parjer", "xxxxxasdax", "BA 1234 BA", "Truck", "Asus", "Jakarta", "A4X", "1234", "12334"))
    list.add(TrackingModel("WO",900000,"Karyawan","Parjip", "xxxxxasdax", "BA 1234 BA", "Truck", "Asus", "Jakarta", "A4X", "1234", "12334"))
    list.add(TrackingModel("WO",1000000000,"Karyawan","Parjan", "xxxxxasdax", "BA 1234 BA", "Truck", "Asus", "Jakarta", "A4X", "1234", "12334"))
    list.add(TrackingModel("WO",5000000,"Karyawan","Parjak", "xxxxxasdax", "BA 1234 BA", "Truck", "Asus", "Jakarta", "A4X", "1234", "12334"))
    adapter!!.notifyDataSetChanged()
}

companion object {
    fun newInstance(): TrackingFragment = TrackingFragment()
}

我跟踪父级是null。还是我有任何错误。如果有人帮助我,我将非常感激。

3 个答案:

答案 0 :(得分:0)

尝试用以下方法替换onCreateViewHolder()方法:

9 - apples 5 - grapes 6 - oranges

答案 1 :(得分:0)

请试试这个:

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): viewHolder {
        val binding : ItemTrackingDataBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),R.layout.item_tracking_data,parent,false)
        return viewHolder(binding)
    }

如果有帮助,请告诉我。

答案 2 :(得分:0)

这非常有趣,我花了差不多整天来解决这个问题..我的错误是在我的自定义xml中,我使用这样的视图。

<view
    android:background="@color/colorGray"
    android:layout_width="match_parent"
    android:layout_height="1dp"/>

没有任何错误,但它的错误观点......正确的是

<View
    android:background="@color/colorGray"
    android:layout_width="match_parent"
    android:layout_height="1dp"/>
希望这有助于某人。