-2

i've seen some answers suggest that i should change to this line of code

android.support.v7.widget.GridLayout

or

androidx.gridlayout.widget.GridLayout

none work. also importing as v7 and as androidx didnt work. please help. it worked fine until now :( thank you!

MainActivity:

package com.example.braindtrainer;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    GridLayout myGridLayout;
    Button goButton;
    TextView timeTextView;
    TextView scoreTextView;
    TextView questionTextView;
    TextView notifyTextView;


    public void goClick(View view){

        Log.i("goButton","Pressed");
        timeTextView.setVisibility(View.VISIBLE);
        scoreTextView.setVisibility(View.VISIBLE);
        questionTextView.setVisibility(View.VISIBLE);
        notifyTextView.setVisibility(View.VISIBLE);
        goButton.setVisibility(View.INVISIBLE);
    }

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

        myGridLayout = findViewById(R.id.myGridLayout);
        goButton = findViewById(R.id.goButton);
        timeTextView = findViewById(R.id.timerTextView);
        scoreTextView = findViewById(R.id.scoreTextView);
        questionTextView = findViewById(R.id.questionTextView);
        notifyTextView = findViewById(R.id.notifyTextView);
    }
}

Layout

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <Button
        android:id="@+id/goButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#B388FF"
        android:onClick="goClick"
        android:text="GO!"
        android:textSize="70dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.gridlayout.widget.GridLayout
        android:id="@+id/myGridLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="300dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="300dp"
        android:visibility="invisible"
        app:columnCount="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:rowCount="2">

        <TextView
            android:id="@+id/firstTextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#991230C8"
            android:tag="1"
            android:text="567"
            android:textColor="#000000"
            android:textSize="100sp"
            app:layout_column="0"
            app:layout_row="0" />

        <TextView
            android:id="@+id/secondTextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#A9C62424"
            android:tag="2"
            android:text="567"
            android:textColor="#000000"
            android:textSize="100sp"
            app:layout_column="1"
            app:layout_row="0" />

        <TextView
            android:id="@+id/thirdTextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#9A60E841"
            android:tag="3"
            android:text="567"
            android:textColor="#000000"
            android:textSize="100sp"
            app:layout_column="0"
            app:layout_row="1" />

        <TextView
            android:id="@+id/fourthTextView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#94CFBF30"
            android:tag="4"
            android:text="567"
            android:textColor="#000000"
            android:textSize="100sp"
            app:layout_column="1"
            app:layout_row="1" />

    </androidx.gridlayout.widget.GridLayout>

    <TextView
        android:id="@+id/timerTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:background="#EA80FC"
        android:text="15 s"
        android:textColor="#000000"
        android:textSize="60sp"
        android:visibility="invisible"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/scoreTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:background="#FFAB40"
        android:text="0/0"
        android:textColor="#000000"
        android:textSize="60dp"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/questionTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="29dp"
        android:layout_marginLeft="29dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="34dp"
        android:layout_marginRight="34dp"
        android:background="#3E3C1515"
        android:text="4 + 6"
        android:textColor="#000000"
        android:textSize="50dp"
        android:visibility="invisible"
        app:layout_constraintEnd_toStartOf="@+id/scoreTextView"
        app:layout_constraintStart_toEndOf="@+id/timerTextView"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/notifyTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="39dp"
        android:text="00000"
        android:textColor="#000000"
        android:textSize="50dp"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/myGridLayout" />

</androidx.constraintlayout.widget.ConstraintLayout>

Log

2020-03-22 18:22:13.472 7929-7929/com.example.braindtrainer E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.braindtrainer, PID: 7929
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.braindtrainer/com.example.braindtrainer.MainActivity}: java.lang.ClassCastException: androidx.gridlayout.widget.GridLayout cannot be cast to android.widget.GridLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.ClassCastException: androidx.gridlayout.widget.GridLayout cannot be cast to android.widget.GridLayout
        at com.example.braindtrainer.MainActivity.onCreate(MainActivity.java:36)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
  • 1
    What is the error that you are seeing in your log? Please post your logcat. Thank you! – Reaz Murshed Mar 21 '20 at 21:18
  • 1
    Also, I think you should post the layout of your activity. – Reaz Murshed Mar 21 '20 at 21:19
  • It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Mar 21 '20 at 22:19

1 Answers1

1

From the crash log is says there is a class cast exception. You used the GridLayout from the androidx library in the xml whereas, you are trying to cast it to the GridLayout from the android library modules in your java class. Hence you are getting this exception. You need to use the same version for both of your layout xml and the java class.

You need to import the following in your Java class.

import androidx.gridlayout.widget.GridLayout

And remove the following

import android.widget.GridLayout;
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Wow thanks! could you please explain what is the difference exactly between android and androidx? And how to i make it use the android gridlayout by default ? because i never encountered this problem. – nragonnragon Mar 24 '20 at 20:06
  • `androidx` is the newer version of libraries that are being introduced by the Android developers' team, whereas, the `android` dependencies are the old versions. If you remove the `androidx` dependencies from your `build.gradle` you should be able to your `GridLayout` as default. Please mark the answer as accepted if that solves your problem. Happy coding! – Reaz Murshed Mar 24 '20 at 20:30