Error: android.widget.LinearLayout can not be cast to android.widget.GridLayout

1

I'm trying to make an android app that lists the components coming from the bank.

The error is as follows:

  

AndroidRuntime: java.lang.ClassCastException: android.widget.LinearLayout can not be cast to android.widget.GridLayout

FILE NAME: HomeActivity.java

private Map<Integer, ViewGroup> ambientes = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home2);


    Bundle extras = getIntent().getExtras();

    int codigo = extras.getInt("codigo");
    int codigo_familia = extras.getInt("codigo_familia");

    Ion.with(this)
            .load("GET", "http://200.145.153.163/houzin/mobile/get_controles_home.php")
            .addQuery("familia", String.valueOf(codigo_familia))
            .asJsonArray()
            .setCallback(new FutureCallback<JsonArray>() {
                @Override
                public void onCompleted(Exception e, JsonArray result) {
                    Toast.makeText(HomeActivity.this, "Aguardando a sincronização com o banco de dados...", Toast.LENGTH_LONG).show();
                    for(final JsonElement obj : result) {
                        final JsonObject json = obj.getAsJsonObject();
                        int k = json.get("codigo_ambiente").getAsInt();
                        //TODO: O ERRO ETÁ AQUI
                        ambientes.computeIfAbsent(k, new Function<Integer, ViewGroup>() {
                            @Override
                            public ViewGroup apply(Integer integer) {
                                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                final ViewGroup rowView = (GridLayout) inflater.inflate(R.layout.layout_ambientes, (LinearLayout) findViewById(R.id.ambientes_home));
                                //TODO: FIM DO ERRO
                                ((TextView) rowView.findViewById(R.id.textView_nomeAmbiente)).setText(json.get("nome_ambiente").getAsString());
                                return rowView;
                            }
                        });

                        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        final Button rowView = (Button) inflater.inflate(R.layout.layout_controles, ambientes.get(k));
                        rowView.setText(json.get("nome_controle").getAsString());
                    }
                }
            });}

The application terminates when you open the page where the components would be listed.

I'm using an xml to make the component rendering buttons.

FILE NAME.XML: layout_controles.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_controle"
android:layout_width="120dp"
android:layout_height="140dp"

android:background="#4ea596"
android:drawableTop="@drawable/ic_air_conditioning_indoor_white"
android:text="Ar Condicionado"
android:textColor="@android:color/white"

/>

This xml of the buttons will be inserted in another xml that represents the environments that these components were.

FILE NAME.XML: layout.ambient.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
>
<TextView
    android:id="@+id/textView_nomeAmbiente"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textColor="#000000"
    android:textSize="20dp"
    android:layout_centerHorizontal="true"
    android:text="NOME DO AMBIENTE"

    />
    </GridLayout>

These environments will be inserted into this xml of the application home page, more specifically within LinearLayout.

FILE NAME.XML: content_home.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HomeActivity"
tools:showIn="@layout/app_bar_home"
>

<RelativeLayout 
    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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="STATUS"
        android:textColor="#000000"
        android:textSize="20dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button1"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView"
        android:background="#494848"
        android:drawableTop="@drawable/ic_temperature"
        android:text="Temp"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/button2"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:background="#494848"
        android:drawableTop="@drawable/ic_thermostat"
        android:text="Gas"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/button3"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignParentRight="true"
        android:layout_below="@id/textView"
        android:background="#494848"
        android:drawableTop="@drawable/ic_drop"
        android:text="UMID"
        android:textColor="#FFFFFF" />

    <LinearLayout
        android:id="@+id/ambientes_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/button3"
        android:orientation="vertical"
        >


    </LinearLayout>

</RelativeLayout>

Gentlemen, now another error has appeared:

  

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.gson.JsonElement.getAsString ()' on a null object reference

     

java.lang.ClassCastException: android.widget.LinearLayout can not be cast to android.widget.Button

    
asked by anonymous 14.09.2018 / 05:33

1 answer

1

LayoutInflater.inflate (int resource, ViewGroup root)

  

Inflates a new view hierarchy of the specified xml resource. It throws InflateException if there is an error.

Resource parameter: ID of an XML layout resource to load (for example, R.layout.main_page)

Root parameter: View optional to be the parent of the generated hierarchy.

Object returned: The root view of the inflated hierarchy. If root was provided, it will be the root view; otherwise, it will be the root of the inflated XML file.

Your line looks like this:

ViewGroup rowView = (GridLayout) inflater.inflate(R.layout.layout_ambientes, (LinearLayout) findViewById(R.id.ambientes_home));

You have provided home_dir_paths as the root parameter, according to the documentation, if you provide this parameter, inflater.inflate(...) will return the root view of the layout, which in this case is LinearLayout since it was supplied, the error happens because you are trying to transform the result from inflater.inflate() to GridLayout

    
15.09.2018 / 00:36