无法解决符号错误 |安卓工作室错误 |房间数据库

问题描述 投票:0回答:1

我正在用 Java 开发一个记事本应用程序。在 RoomDB.java 类中初始化数据库后,我在 MainDAO.java 接口中导入值以执行数据库逻辑。但是,我在 MainDAO.java 文件的“问题”框中遇到以下错误: Here 无法解析符号“ID” 无法解析符号“注释” 无法解析符号“标题” 无法解析符号“注释”

我还在学习在android studio中开发应用程序所以请指导我并让我知道错误的原因。以下是供您参考的代码文件,如果您需要其他任何东西来调试此错误,请告诉我。

MainDAO.java

package com.example.notepad_colornotes.Database;

import static androidx.room.OnConflictStrategy.REPLACE;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;

import com.example.notepad_colornotes.Models.Notes;


import java.util.List;

@Dao
public interface MainDAO {
    //method to insert data into our database
    @Insert(onConflict = REPLACE)
    void insert(Notes notes);

    //method to get all the data, datatype is list
    @Query("SELECT * FROM notes ORDER BY ID DESC") //this query arranges the items in the notes in descending order. newly added ones will be at the top
    List<Notes> getAll();

    //method to update the data
    @Query("UPDATE notes SET title = :Title, notes = :Notes WHERE ID = :ID") //takes the value from the model and that of the update method parameter
    void update(int ID, String Title, String Notes);

    @Delete
    void delete(Notes notes);
}

房间数据库.java

package com.example.notepad_colornotes.Database;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.example.notepad_colornotes.Models.Notes;

@Database(entities = Notes.class, version = 1, exportSchema = false)
public abstract class RoomDB extends RoomDatabase {
    private static RoomDB database;
    private static String DATABASE_NAME = "NoteApp";

    //method to get the instance of the database
    public synchronized static RoomDB getInstance(Context context){
        if (database == null){
            database = Room.databaseBuilder(context.getApplicationContext(),
                    RoomDB.class, DATABASE_NAME)
                    .allowMainThreadQueries()
                    .fallbackToDestructiveMigration()
                    .build();
        }
        return database;
    }

    //instance of DAO
    public abstract MainDAO mainDAO();
}

我也尝试将 compilersdkversion 设置为 33,我还重建了项目,我还检查了导入和值的大小写敏感性。还是没用

java android android-studio dao cannot-find-symbol
1个回答
0
投票

我不是很熟悉你在 RoomDB.java 中使用的代码,因为我只使用了一种非常具体的方法来做这些事情。然而,不确定它是否会改变任何东西,但是你可以在你的 DAO 中改变一些东西。

首先,Room 中有一个更新符号,因此您不必使用查询来更新表。

import androidx.room.Update; //this should go where your other import statements are

@Update
public void updateAgent(Agent agent); //example code from one of my projects - this should go where you currently have the Query for updating the tables

其次,正如您在上面看到的,我为每个方法声明了 public 和 void。不确定这是否会解决您的问题,但试一试也不会有什么坏处。 (下面我的项目的示例代码)

package com.example.excursiontime;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface MyDAO {

    //Agents
    @Insert
    public void addAgent(Agent agent);

    @Query("SELECT * FROM agents")
    public List<Agent> getAgents();

    @Delete
    public void deleteAgent(Agent agent);

    @Update
    public void updateAgent(Agent agent);
}

PS:很抱歉回答这个问题。我没有足够的声誉在评论中问你所有这些。如果这确实解决了您的问题,不妨将它作为答案放在这里,以便其他人可以看到它。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.