我正在学习一个教程,在该教程中,您将构建一个带有回收器视图的笔记应用程序。在我添加适配器并在HomeFragment中引用它之后,我开始出现两个错误。
我还仔细检查了类/变量等的大小写。尝试了小写和大写。也尝试过导入R。以下是我不断得到的两个错误:
error: cannot find symbol
public final NotesAdapter getNotesAdapter() {
^symbol:
class NotesAdapter
location: class HomeFragment
error: cannot find symbol
private NotesAdapter notesAdapter;
^
symbol: class NotesAdapter
location: class HomeFragment
这是我的HomeFragment导入包括:
package com.denizgocer.notetakingfinal
import com.denizgocer.notetakingfinal.R
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SearchView
import android.widget.Toast
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.denizgocer.notetakingfinal.database.NotesDatabase
import com.denizgocer.notetakingfinal.entities.Notes
import kotlinx.android.synthetic.main.fragment_create_note.*
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.coroutines.launch
import NotesAdapter
import java.util.*
import kotlin.collections.ArrayList
class HomeFragment : BaseFragment() {
var arrNotes = ArrayList<Notes>()
var notesAdapter: NotesAdapter = NotesAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragment_home,
container,
false
)
}
companion object {
@JvmStatic
fun newInstance() =
HomeFragment().apply {
arguments = Bundle().apply {
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recycler_view.setHasFixedSize(true)
recycler_view.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
launch {
context?.let {
var notes = NotesDatabase.getDatabase(it).noteDao().getAllNotes()
notesAdapter!!.setData(notes)
arrNotes = notes as ArrayList<Notes>
recycler_view.adapter = notesAdapter
}
}
notesAdapter!!.setOnClickListener(onClicked)
fabCreateNote.setOnClickListener {
replaceFragment(CreateNoteFragment.newInstance(),false)
}
search_view.setOnQueryTextListener( object : SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(p0: String?): Boolean {
return true
}
override fun onQueryTextChange(p0: String?): Boolean {
var tempArr = ArrayList<Notes>()
for (arr in arrNotes){
if (arr.title!!.toLowerCase(Locale.getDefault()).contains(p0.toString())){
tempArr.add(arr)
}
}
notesAdapter.setData(tempArr)
notesAdapter.notifyDataSetChanged()
return true
}
})
}
private val onClicked = object :NotesAdapter.OnItemClickListener{
override fun onClicked(notesId: Int) {
var fragment :Fragment
var bundle = Bundle()
bundle.putInt("noteId",notesId)
fragment = CreateNoteFragment.newInstance()
fragment.arguments = bundle
replaceFragment(fragment,false)
}
}
fun replaceFragment(fragment:Fragment, istransition:Boolean){
val fragmentTransition = activity!!.supportFragmentManager.beginTransaction()
if (istransition){
fragmentTransition.setCustomAnimations(android.R.anim.slide_out_right,android.R.anim.slide_in_left)
}
fragmentTransition.replace(R.id.frame_layout,fragment).addToBackStack(fragment.javaClass.simpleName).commit()
}
}
这是我的NotesAdapter,包括进口:
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.denizgocer.notetakingfinal.R
import com.denizgocer.notetakingfinal.entities.Notes
import kotlinx.android.synthetic.main.item_rv_notes.view.*
import java.util.*
import kotlin.collections.ArrayList
class NotesAdapter :
RecyclerView.Adapter<NotesAdapter.NotesViewHolder>() {
var listener: OnItemClickListener? = null
var arrList = ArrayList<Notes>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NotesViewHolder {
return NotesViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_rv_notes, parent, false)
)
}
override fun getItemCount(): Int {
return arrList.size
}
fun setData(arrNotesList: List<Notes>) {
arrList = arrNotesList as ArrayList<Notes>
}
fun setOnClickListener(listener1: OnItemClickListener) {
listener = listener1
}
override fun onBindViewHolder(holder: NotesViewHolder, position: Int) {
holder.itemView.tvTitle.text = arrList[position].title
holder.itemView.tvDesc.text = arrList[position].noteText
holder.itemView.tvDateTime.text = arrList[position].dateTime
if (arrList[position].color != null) {
holder.itemView.cardView.setCardBackgroundColor(Color.parseColor(arrList[position].color))
} else {
holder.itemView.cardView.setCardBackgroundColor(Color.parseColor(R.color.ColorLightBlack.toString()))
}
if (arrList[position].pathImage != null) {
holder.itemView.imgNote.setImageBitmap(BitmapFactory.decodeFile(arrList[position].pathImage))
holder.itemView.imgNote.visibility = View.VISIBLE
} else {
holder.itemView.imgNote.visibility = View.GONE
}
if (arrList[position].webLink != "") {
holder.itemView.tvWebLink.text = arrList[position].webLink
holder.itemView.tvWebLink.visibility = View.VISIBLE
} else {
holder.itemView.tvWebLink.visibility = View.GONE
}
holder.itemView.cardView.setOnClickListener {
listener!!.onClicked(arrList[position].pld!!)
}
}
class NotesViewHolder(view: View) : RecyclerView.ViewHolder(view)
interface OnItemClickListener {
fun onClicked(noteId: Int)
}
}
似乎NotesAdapter
在默认包中。这意味着您没有明确地放入包中,而是直接放入源文件夹中。
只有默认程序包中的类才能使用默认程序包的其他类。您不能在其中import
类。
为了解决这个问题,请将NotesAdapter
放入包中并正确导入。
此外,您可能需要使用File
>Invalidate Caches and Restart
>Invalidate and Restart
如果在将类移动到包后错误仍然存在,因为AndroidStudio并不总是正确检测到以及处理源文件的移动(根据我的经验(。