java.lang.IllegalStateException:itemActivity.item_rv(recyclerview)不得为空

时间:2019-02-18 14:02:24

标签: android-recyclerview

我有2个活动,一个显示带有图像的类别,单击类别会跳到出现此问题的项目列表活动,两个活动的编码几乎相同。请告知。

ItemActivity

ItemActivity类:AppCompatActivity(),NavigationView.OnNavigationItemSelectedListener {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_item)
    setSupportActionBar(toolbar)

   // var catid:Int = intent.getIntExtra("CATEGORYID")
   Toast.makeText(this, UserInfo.categor_id.toString(), Toast.LENGTH_LONG).show()

    var url = "http://machineIP/gocart/get_item.php?cat_id=" + UserInfo.categor_id
    var list=ArrayList<item>()
    var rq: RequestQueue = Volley.newRequestQueue(this)
    var jar= JsonArrayRequest(Request.Method.GET,url,null, Listener{ response ->
        for(i in 0..response.length()-1)
            list.add(item(response.getJSONObject(i).getInt("item_id"),response.getJSONObject(i).getString("item_name"),response.getJSONObject(i).getString("item_desc"),response.getJSONObject(i).getString("item_image"),response.getJSONObject(i).getDouble("item_sell_price")))
        var adp= ItemAdapter(this,list)
        item_rv.layoutManager = LinearLayoutManager(this)
        item_rv.adapter = adp
    }, Response.ErrorListener{ error ->
        Toast.makeText(this,error.message, Toast.LENGTH_LONG).show()
    })

    rq.add(jar)

ItemAdaptor

class ItemAdapter(变量上下文:Context,变量列表:ArrayList):RecyclerView.Adapter(){     重写fun onBindViewHolder(p0:RecyclerView.ViewHolder,p1:Int){         (p0为ItemHolder).bind(list [p1] .itemId,list [p1] .itemName,list [p1] .itemDesc,list [p1] .itemImage,list [p1] .itemSalePrice)     }

override fun getItemCount(): Int {
    return list.size
}

override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
    var v: View = LayoutInflater.from(context).inflate(R.layout.item_row,p0,false)
    return ItemHolder(v)
}


class ItemHolder(itemView: View): RecyclerView.ViewHolder(itemView){
    fun bind(itemid:Int,n:String,d:String,i:String,p:Double)
    {
        itemView.item_name.text=n
        itemView.item_desc.text=d
        itemView.item_sell_price.text=p.toString()
        Picasso.get().load("http://machineIP/gocart/images/"+i).into(itemView.item_image)
    }
}

} 让我知道,如果还有其他需要...。我是android开发的新手。...

enter image description here

0 个答案:

没有答案