【问题标题】:recyclerview must not be nullrecyclerview 不能为空
【发布时间】:2020-11-27 05:17:33
【问题描述】:
Process: com.bignerdranch.android.on_callbabysitting, PID: 19624
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.on_callbabysitting/com.bignerdranch.android.on_callbabysitting.Home}: java.lang.IllegalStateException: recyclerv_view must not be null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.IllegalStateException: recyclerv_view must not be null
        at com.bignerdranch.android.on_callbabysitting.Home.onCreate(Home.kt:42)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    tools:context=".Home">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerv_view"
        android:layout_width="match_parent"
        android:layout_height ="match_parent">

    </androidx.recyclerview.widget.RecyclerView>

</RelativeLayout>

package com.bignerdranch.android.on_callbabysitting

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.View.inflate
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.recycv1.*
import kotlinx.android.synthetic.main.sidebar.*
import java.lang.IllegalStateException

class Home : AppCompatActivity() {


    var names = ArrayList<String>()
    var imgUrls = ArrayList<String>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.homepage)
        lateinit var context: Context

        var helper = MyDBHelper(applicationContext)
        var db = helper.readableDatabase
        var rs = db.rawQuery("SELECT * FROM USERS", null)

        rs.moveToFirst()
        while(rs.isAfterLast == false)
        {
            names.add(rs.getString(rs.getColumnIndex("EMAIL")))
            rs.moveToNext()
            names.add(rs.getString(rs.getColumnIndex("FIRST_NAME")))
            rs.moveToNext()
            //names.add(rs.getString(rs.getColumnIndex("LAST_NAME")))
            //rs.moveToNext()
        }
        recyclerv_view.layoutManager = LinearLayoutManager(this)
        recyclerv_view.adapter = RecyclerViewAdapter(imgUrls, names, this)
        /*names.add(editTextTextEmailAddress3.getText().toString())
        names.add(editTextTextPassword.getText().toString())
        //names.add(editTextTextEmailAddress3.getText().toString())
        //names.add(editTextTextPassword.getText().toString())*/

        //try {
           // recyclerv_view.adapter = RecyclerViewAdapter.First(this, imgUrls, names)
            //recyclerv_view.layoutManager = LinearLayoutManager(this)
       // }catch (ignored: IllegalStateException)
        //{

        //}
        //val recyclerView : RecyclerView = findViewById(R.id.recyclerv_view)
        //val recyclerView = findViewById<RecyclerView>(R.id.recyclerv_view)
        //recyclerView.setAdapter(first)
        //recyclerView.setLayoutManager(LinearLayoutManager(this))
    }


}



package com.bignerdranch.android.on_callbabysitting

import android.app.PendingIntent.getActivity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.recyclerview.widget.RecyclerView
import de.hdodenhof.circleimageview.CircleImageView
import android.widget.TextView
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import java.security.AccessController.getContext

open class RecyclerViewAdapter(val imgNames: ArrayList<String>, val imgPic: ArrayList<String>, val context: Context) : RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>() {

    //var Context = context

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
    {

       // lateinit var cimg:CircleImageView
        //lateinit var img:TextView

        //lateinit var parentLayout: LinearLayout

        val CircleImageView = itemView.findViewById<CircleImageView>(R.id.profile_image)
        val TextView = itemView.findViewById<TextView>(R.id.textView19)
        val TextView1 = itemView.findViewById<TextView>(R.id.textView18)
        val TextView2 = itemView.findViewById<TextView>(R.id.textView20)
        val parentLayout = itemView.findViewById<LinearLayout>(R.id.parent_layout)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        //var View = LayoutInflater.from(parent.getContext()).inflate(R.layout.homepage, parent, false)
        //var viewHolder = ViewHolder(View)
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.homepage, parent, false))
    }

    override fun getItemCount(): Int {

        return imgNames.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {


       /* Glide
            .with(context)
            .asBitmap()
            .load(imgPic.get(position))
            .into(holder.CircleImageView)

        var First = First(context,imgNames, imgPic)
        First.imgN.add(imgNames.get(position))

        */
        holder?.TextView?.text = imgNames.get(position)
        holder?.TextView1.text = imgNames.get(position)
        holder?.TextView2.text = imgNames.get(position)
        holder?.parentLayout.parent
    }
}

我试图让我的代码在登录主页时显示用户名,我可以通过单击成功地从一个活动移到下一个活动,但是当我在我的主页活动中添加回收站视图时,它不会不行。附件是回收器视图 XML、适配器活动和我的家庭类活动以及日志。

【问题讨论】:

    标签: android android-studio kotlin android-recyclerview


    【解决方案1】:

    在您的导入中,导入了多个 UI。只需检查一次 recyclerview 的实际位置即可。

     import kotlinx.android.synthetic.main.activity_main.*
     import kotlinx.android.synthetic.main.recycv1.*
     import kotlinx.android.synthetic.main.sidebar.*
    

    【讨论】:

    • 我不明白你的意思。能不能说的更直白一点?
    • xml recyclerview在哪?
    • Recycv1 是 xml
    【解决方案2】:

    你还没有在活动类中初始化recyclrv_view。

    【讨论】:

    • 谢谢。所以我必须在活动主体中初始化?通过使用 findbyid?
    • 在使用 kotlin 时不需要 findviewbyid
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 2015-09-14
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多