Error: android.widget.Linearlayout cannot be cast to android.widget.Gridlayout

Asked

Viewed 51 times

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 cannot 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 ends when it opens the page where the components would be listed.

I am using an xml to make the component representatives buttons.

XML FILE NAME: 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 have been.

XML FILE NAME: layout_ambientes.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 in this application’s HOME page xml, more specifically within Linearlayout.

XML FILE NAME: 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 mistake 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 cannot be cast to android.widget.Button

  • Could put a caption indicating which name of the XML file is displaying the code?

  • All right, see if that’s how you like it.

1 answer

1


Layoutinflater.inflate(int Resource, Viewgroup root)

Inflates a new hierarchy of views of the specified Resource xml. Launches Inflateexception if there is an error.

Resource parameter: ID of an XML layout Resource to load (e.g., R.layout.main_page)

Root parameter: Optional view to be the parent of the generated hierarchy.

Returned object: The root view of the inflated hierarchy. If root has been provided, it will be the root view; otherwise it will be the root of the inflated XML file.

Your line is like this:

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

You provided R.id.ambientes_home as the root parameter, according to the documentation, if you provide this parameter, inflater.inflate(...) return the root view of the layout, which in this case is a LinearLayout since it was provided, the error happens because it is trying to transform the result of inflater.inflate() in a GridLayout

Browser other questions tagged

You are not signed in. Login or sign up in order to post.