Skip to content

Java problem, run time error, maybe problem occurs on numbers activity.xml file, tried to configure problem but still app is not working [closed]

MainActivity.java

package com.example.marathiiseasy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import static com.example.marathiiseasy.R.id.numbers_textview;

public class MainActivity extends AppCompatActivity {

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

        TextView number = (TextView) findViewById(numbers_textview);
        number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent numberintent = new Intent(MainActivity.this, numbersActivity.class);
                startActivity(numberintent);
            }
        });
    }

activity_main.xml

<?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">
    <TextView
        android:id="@+id/numbers_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Numbers"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.09"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.087"
       
        />
</androidx.constraintlayout.widget.ConstraintLayout>`

numberActivity.java

package com.example.marathiiseasy;

import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class numbersActivity extends AppCompatActivity {


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


        ArrayList<String> numberswords = new ArrayList<String>();
        numberswords.add("one");
        numberswords.add("two");
        numberswords.add("three");
        numberswords.add("four");
        numberswords.add("five");
        numberswords.add("six");
        numberswords.add("seven");
        numberswords.add("eight");
        numberswords.add("nine");
        numberswords.add("ten");
        Log.v("numbersActivity","wprd at index 0"+ numberswords.get(0));
        //to connect numbers method in main xml file to this numberactivity.xml file
        LinearLayout numbersrootview;
        numbersrootview = (LinearLayout) findViewById(R.id.numbers_roottextview);

        for (int index =0;index<numberswords.size(); index++){
            TextView numobj = new TextView(this);
            numobj.setText(numberswords.get(index));
            numbersrootview.addView(numobj);
        }
    }
}

activity_numbers.xml

<?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=".numbersActivity"
    android:id="@+id/numbers_roottextview">
</androidx.constraintlayout.widget.ConstraintLayout>

Debugging error (in Logcat)=

2021-04-30 19:03:52.716 5583-5583/com.example.marathiiseasy E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.marathiiseasy, PID: 5583
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.marathiiseasy/com.example.marathiiseasy.numbersActivity}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.LinearLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6651)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
     Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.LinearLayout
        at com.example.marathiiseasy.numbersActivity.onCreate(numbersActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:7088)
        at android.app.Activity.performCreate(Activity.java:7079)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:176) 
        at android.app.ActivityThread.main(ActivityThread.java:6651) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 
2021-04-30 19:03:52.858 5583-5583/com.example.marathiiseasy I/Process: Sending signal. PID: 5583 SIG: 9

numbersactivity.java file

numbersactivity.xml file

mainactivity.java file mainactivity.xml file

Help me find the problem please it may be simple error but don’t know what is causing app to crash, Its an app which represents numbers in words using intent, But when I click on number textview(which has the intent for) the app crashes before displaying the words for number… Please find the run time error I want to move forward with this app. Please help me please………………

Answer

your numbers_roottextview is a ConstraintLayout in your number_activity.xml but when you declare it in the java file you’re casting it to LinearLayout which results in the java.lang.RuntimeException:

java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.LinearLayout

So you should make it LinearLayout in the xml file