0

The error contains: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView

It worked well when I first debugged it, but now it doesn't work because of the error. I first copied the code and brought it as it is, but it doesn't work. So I have no idea which part is the problem.

This is my .java file:

package com.example.season;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class SelectSeason extends AppCompatActivity {

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

        ListView listview = (ListView)findViewById(R.id.seasonlist);

        List<String> SeasonList = new ArrayList<>();

        ArrayAdapter<String> SeasonAdapter = new ArrayAdapter<>(this, R.layout.activity_select_season, SeasonList);

        listview.setAdapter(SeasonAdapter);

        SeasonList.add("Spring");
        SeasonList.add("Summer");
        SeasonList.add("Fall");
        SeasonList.add("Winter");

    }
}

This is my .xml 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="com.example.kokona_rollbook.SelectSeason">

    <TextView
        android:id="@+id/seasontitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="SEASON"
        android:textSize="20dp"
        />

    <ListView
        android:id="@+id/seasonlist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_below="@id/seasontitle"/>

</LinearLayout>

Thank you

  • That's coming from the `ArrayAdapter`. You've passed the wrong `R.layout` there. `R.layout.activity_select_season` is your `Activity`'s layout. You need to pass the `R.layout` for the `ListView` items. – Mike M. Jun 03 '20 at 03:37
  • use lower case for variables (seasonList), consider use RecyclerView instead ListView, notify your adapter after adding more itens – Acauã Pitta Jun 03 '20 at 03:41

0 Answers0