【发布时间】: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