`findViewById` 无法在 `activity_main` 中找到 RecyclerView。

huangapple go评论60阅读模式
英文:

findViewById cannot find RecyclerView in activity_main

问题

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridView = findViewById(R.id.unitsGrid);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, units);

    recyclerView = findViewById(R.id.recyclerView);
}

activity_main.xml

<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Activities.MainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView></androidx.appcompat.widget.LinearLayoutCompat>
英文:

I can find other layouts using id , but i am unable to find RecyclerView

MainActivity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    GridView gridView=findViewById(R.id.unitsGrid);
    ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,
            android.R.layout.simple_list_item_1, units);

    recyclerView= findViewById(R.id.);

}

activiy_main.xml

&lt;androidx.appcompat.widget.LinearLayoutCompat xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.Activities.MainActivity&quot;&gt;
&lt;androidx.recyclerview.widget.RecyclerView
    android:id=&quot;@+id/recyclerView&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

&lt;/androidx.recyclerview.widget.RecyclerView&gt;&lt;/androidx.appcompat.widget.LinearLayoutCompat&gt;

答案1

得分: 0

有时复制/粘贴xml项目时会出现此问题,执行“文件 > 无效缓存/重启”并尝试。

英文:

Some time when copy/paste xml items, this issue occurs, do File &gt; Invalidate Caches / Restart and try.

huangapple
  • 本文由 发表于 2020年4月11日 06:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61149623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定