Home > front end >  android.widget.LinearLayout cannot be cast to android.widget.TextView at com.example.foodorderapp.Ad
android.widget.LinearLayout cannot be cast to android.widget.TextView at com.example.foodorderapp.Ad

Time:04-29

I'm Trying To Solve This Bug At Debugging But Couldn't Find Its Solution Souce Code Link: Download


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.foodorderapp.Models.OrdersModel;
import com.example.foodorderapp.R;

import java.util.ArrayList;

public class OrdersAdapter extends RecyclerView.Adapter<OrdersAdapter.viewHolder>
{
    ArrayList<OrdersModel> list;
    Context context;


    public OrdersAdapter(ArrayList<OrdersModel> list, Context context) {
        this.list = list;
        this.context = context;
    }


    @NonNull
    @Override
    public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        View view= LayoutInflater.from(context).inflate(R.layout.order_sample,parent,false);
        return new viewHolder(view);

    }


    @Override
    public void onBindViewHolder(@NonNull viewHolder holder, int position) {

        final OrdersModel model=list.get(position);
        holder.orderImage.setImageResource(model.getOrderImage());
        holder.soldItemName.setText(model.getSoldItemName());
        holder.orderNumber.setText(model.getOrderNumber());
        holder.price.setText(model.getPrice());

    }

    @Override
    public int getItemCount()
    {

        return list.size();
    }

    public class viewHolder extends RecyclerView.ViewHolder {

        ImageView orderImage;

        TextView soldItemName,orderNumber,price;

        public viewHolder(@NonNull View itemView) {
            super(itemView);

            orderImage=itemView.findViewById(R.id.orderImage);
            soldItemName=itemView.findViewById(R.id.orderItemName);
            orderNumber=itemView.findViewById(R.id.orderNumber);
            price=itemView.findViewById(R.id.orderPrice);

        }
    }


}

When I Try To Run App Works Properly But Specific In This Activity Their No Any Error But App Can't Run & When I Debug App Then I Get This Error java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView

Souce Code Link: Download

CodePudding user response:

Remove the ID from Linearlayout and add to your textview-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    **android:id="@ id/orderItemName"** //REMOVE THIS and add to your TEXTVIEW
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:orientation="vertical"
    android:padding="1dp">

CodePudding user response:

The answer is in the error/title of this post.

Problem:

It says LinearLayout cannot be cast as TextView, which means somewhere in your code, you're trying to use and cast a LinearLayout as a TextView which is inherently wrong as a LinearLayout can't be used a TextView.

Reason:

This is the layout you've created in order_sample.xml

<?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"
    android:id="@ id/orderItemName"
   ...>
...
</LinearLayout>

You've given this LinearLayout an id named orderItemName.

But, in your Adapter's viewHolder, you're trying to set this LinearLayout to a TextView here:

TextView soldItemName;
...
soldItemName=itemView.findViewById(R.id.orderItemName);

Solution:

Remove the id orderItemName from the LinearLayout and assign it to the respective TextView.

And, your app will work.

  • Related