java无法在recyclerview上设置适配器,错误无法解析“setAdapter”
我一直在尝试在主页上设置适配器,但无法使其正常工作。setAdapter,它总是以Cannot resolve symbol 'setAdapter'
的形式出现
我已尝试从以下位置更改导入:
implementation '安卓x.recyclerview:recyclerview:1.0.0'
致:
implementation 'com.安卓.support:recyclerview-v7:28.0.0'
但似乎都不管用
@Override
protected void onCreate(Bundle savedInstanceState) {
rv = findViewById(R.id.rvtop);
RecyclerView.LayoutManager mRv;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feiticos);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
DatabaseHandler databaseHandler = new DatabaseHandler(this);
mRv = new GridLayoutManager(this.getBaseContext(),1);
Context context;
DatabaseHandler banco = new DatabaseHandler(this);
SQLiteDatabase db = banco.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from feiticos", new String[]{});
cursor.moveToFirst();
do {
spell_nome.add(cursor.getString(1));
spell_nivel.add(cursor.getString(2));
spell_cast_time.add(cursor.getString(3));
spell_range.add(cursor.getString(4));
spell_duration.add(cursor.getString(5));
spell_classe.add(cursor.getString(6));
}
while (cursor.moveToNext());
}
FeiticosRecycler adapter = new FeiticosRecycler(this,spell_nome,spell_nivel,spell_cast_time,spell_range,spell_duration,spell_classe);
rv.setAdapter(adapter);
我希望让setAdapter开始工作,因为我已经在另一个项目中做过一次,我希望它能再次工作,在另一个项目中,我使用的是安卓.support:recyclerview-v7:28.0.0
,而不是安卓x.recyclerview:recyclerview:1.0.0
# 1 楼答案
在设置内容视图之前,您正在使用findViewById查找recyclerview的视图。这就是它找不到recyclerview所需视图的原因
将这些行更改为此顺序