不兼容的类型 - Android Gridview

Incompatible Types - Android Gridview

不兼容的类型。找到:'android.view.View',需要:'com.example.listviewgridview.GridView'。我无法访问 java 文件中的 gridview。我该如何解决这个问题?

GridView.java.

package com.example.listviewgridview;

import androidx.appcompat.app.AppCompatActivity;
import com.example.listviewgridview.GridView;
import android.os.Bundle;


public class GridView extends AppCompatActivity {

String[] language_names;
{
    language_names = new String[]{"Android Studio", "Angular JS", "BootStrap", "C", "CSS", "Github",
            "HTML", "JAVA", "JavaScript", "JQuery", "Nodejs", "PHP", "Python", "React", "Ruby", "Sql", "Wordpress"};
}

GridView gridView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grid_view);

    gridView = findViewById(R.id.main);

}

}

Activity 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".ListView">


<GridView
    android:id="@+id/main"
    android:verticalSpacing="3dp"
    android:numColumns="2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" >

</GridView>
</LinearLayout>

您从错误的地方导入了 Java 中的 GridView。因为您的 Class 名称与 android 默认 GridView 的名称相同。您正在将自己的 class 导入到同一个 class.

解决方案:

  1. 只需将 Class 的名称更改为 GridViewExample
  2. 将导入更改为 android.widget.GridView 代替 com.example.listviewgridview.GridView.