Search code examples
javaandroidgridviewandroid-gridview

Incompatible Types - Android Gridview


Incompatible types. Found: 'android.view.View', required: 'com.example.listviewgridview.GridView'. I am not able to access my gridview in java file. How can I resolve this issue?

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 file

<?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>

Solution

  • You have imported the GridView in Java from the wrong place. Because Your Class name has the same name as the android default GridView. And you are importing your own class into the same class.

    Solutions:

    1. Simply change the name of your Class to GridViewExample
    2. change the import to android.widget.GridView instead of com.example.listviewgridview.GridView.