如何在构建时修复“找不到符号”错误?

Posted

技术标签:

【中文标题】如何在构建时修复“找不到符号”错误?【英文标题】:How do I fix the "cannot find symbol" error while building? 【发布时间】:2021-07-30 18:55:47 【问题描述】:

我正在尝试按照教程构建一个带有回收站视图的笔记应用程序。在我添加适配器并在我的 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)
    


【问题讨论】:

【参考方案1】:

NotesAdapter 似乎在默认包中。这意味着您没有明确地放入包中,而是直接放入源文件夹中。

只有默认包中的类可以使用默认包中的其他类。您不能在其中 import 类。

为了解决这个问题,请将NotesAdapter放入一个包中并正确导入。

此外,如果在将类移动到包后错误仍然存​​在,您可能需要使用File>Invalidate Caches and Restart>Invalidate and Restart,因为 AndroidStudio 并不总是能正确检测到 并处理源文件的移动(根据我的经验)。

【讨论】:

以上是关于如何在构建时修复“找不到符号”错误?的主要内容,如果未能解决你的问题,请参考以下文章

“找不到符号”或“无法解析符号”错误是啥意思?

“找不到符号”或“无法解析符号”错误是啥意思?

“找不到符号”或“无法解析符号”错误是啥意思?

“找不到符号”或“无法解析符号”错误是啥意思?

如何抛出 RuntimeException(“找不到符号”)

如何修复“java:找不到符号”