[英]RecyclerView “Cannot resolve symbol” error
I'm trying to setup a RecyclerView in my Android Project but i cannot refer to my list because using the code "recyclerView = view.findViewById(R.id.list)" give me the error Cannot resolve symbol "list". 我正在尝试在Android项目中设置RecyclerView,但是我无法引用我的列表,因为使用代码“ recyclerView = view.findViewById(R.id.list)”会给我错误,无法解析符号“列表”。 If I try to type "R.id."
如果我尝试输入“ R.id”。 i cannot find R.id.list but I'm sure that I give it the id "list".
我找不到R.id.list,但是我可以确定它的ID为“ list”。
I just added dependencies and the import statement in my code. 我刚刚在代码中添加了依赖项和import语句。 I tried to resync and clean project but with no result.
我试图重新同步和清理项目,但没有结果。
here my dependencies (build.gradle (app)): 这是我的依赖项(build.gradle(app)):
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
here my import: 这是我的导入:
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.app.AppCompatActivity;
here the wrong code: 这里的错误代码:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recyclerView = view.findViewById(R.id.list);
}
here the declaration of the RecyclerView in the xml file: 这是xml文件中RecyclerView的声明:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
Thank you in advance! 先感谢您!
Try File > Invalidate Caches / Restart 尝试“文件”>“使缓存无效/重新启动”
Also, shift this code to onCreateView() 另外,将此代码转移到onCreateView()
recyclerView = view.findViewById(R.id.list);
Where view should be the view you get after inflating. 视图应该是充气后获得的视图。
关闭项目,然后尝试使用rm -r /pathToYourProject/.gradle删除.gradle和.idea,然后打开项目并进行同步
Try in your activity: 尝试您的活动:
import your.project.name.R;
Hope it helps. 希望能帮助到你。
you forget somthing 你忘了东西
just set this line before recyclerView = view.findViewById(R.id.list);
只需在
recyclerView = view.findViewById(R.id.list);
之前设置此行recyclerView = view.findViewById(R.id.list);
setContentview(R.layout.THE_LAYOUT_THAT_CONTAIN_YOUR_RECYCLERVIEW);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.