Android Studio Error: class, interface, or enum expeted

Android Studio Error: class, interface, or enum expeted

我正在尝试在我的数据库中插入测试数据,但没有成功。

观看此视频作为参考,但没有成功:https://www.youtube.com/watch?v=RPi7ueKwEXg

见下文:

package com.bytemeta.bytenota.dominio;

import android.content.ContentValues;
import android.content.Context;
import android.database.*;
import android.database.sqlite.*;
import android.widget.ArrayAdapter;
import android.widget.*;

public class RepositorioCadastro{
    private SQLiteDatabase conn;
    public RepositorioCadastro(SQLiteDatabase conn){
        this.conn = conn;
    }
        public void testeInserirCadastro(){
            for (int i = 0; i < 10; i++){
                ContentValues values = new ContentValues();
                values.put("NOME", "THIAGO");
                conn.insertOrThrow("CADASTRO", null, values);
            }
        }
    }

    public ArrayAdapter<String> buscaCadastro(Context context){

        ArrayAdapter<String> adpCadastro = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);
        Cursor cursor = conn.query("BYTENOTA_DB",null,null,null,null,null,null,null);

        if (cursor.getCount() > 0) {

            cursor.moveToFirst();
            do {
                String NOME = cursor.getString(1);
                adpCadastro.add(NOME);
            }while (cursor.moveToNext());
        }
        return adpCadastro;

    }

}

Android 当您的代码不在 class 声明中时,Studio 会抛出该错误。 您的 public ArrayAdapter<String> buscaCadastro 现在不在 class 中。删除 testeInserirCadastro 后多余的右花括号,应该可以解决这个问题。

我发现这有编码问题。

将您的源代码复制到某处并使其成为 UTF-8 格式。然后,再次将应用的代码用于您的项目。

您还可以在 Android Studio 底部更改编码格式。在这种情况下,您需要将其更改为 UTF-16,然后再次保存为 UTF-8 并保存。