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