Mike
Mike

Reputation: 1

Broadcast Receivers Android

How to fix these problems ?

Please help me and its keeps stopping doesn't work

(crashing)

I'm using 12.0 Api 31

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.4, PID: 23001
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.4/com.example.4.MainActivity}: java.lang.ClassCastException: com.example.4.MainActivity cannot be cast to android.app.Activity
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3549)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3796)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2214)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.ClassCastException: com.example.4.MainActivity cannot be cast to android.app.Activity
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1273)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3536)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3796) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2214) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:201) 
        at android.os.Looper.loop(Looper.java:288) 
        at android.app.ActivityThread.main(ActivityThread.java:7842) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 

MainActivity.java :

package com.example.4;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MainActivity extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
    }
}

AndroidMainfest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.4">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.4">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            tools:ignore="Instantiatable">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="com.tutorialspoint.CUSTOM_INTENT">
                </action>
            </intent-filter>

        </receiver>
    </application>

</manifest>

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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="Example of Broadcast"
            android:textSize="30dp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imageButton"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="40dp"
            android:text="Tutorials point "
            android:textColor="#ff87ff09"
            android:textSize="30dp" />

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/su"
            tools:ignore="SpeakableTextPresentCheck" />

        <Button
            android:id="@+id/button2"
            android:layout_width="268dp"
            android:layout_height="145dp"
            android:layout_below="@+id/imageButton"
            android:layout_centerHorizontal="true"
            android:onClick="broadcastIntent"
            android:text="Broadcast Intent" />

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

No need to read this its because i can don't publish my post because its most of it code so i'm writing for this reason i hope that i can fix my problem this is the fifth hour on my screen

Upvotes: 0

Views: 205

Answers (1)

Darkman
Darkman

Reputation: 2981

java.lang.ClassCastException: com.example.4.MainActivity cannot be cast to android.app.Activity

That exception tells you that the MainActivity is not a valid child of Activity class but BroadcastReceiver or Service. Looking at the Manifest.xml, you have 2 classes with the same name. One for the real activity and another one for a service that you call MainActivity. MainActivity is a class that extends either Activity or AppCompatActivity. And I'd suggest you to use a better name for the BroadcastReceiver. Something like this:

public class Receiver extends BroadcastReceiver {

And the Manifest.xml would look like this:

<receiver android:name="Receiver"
    android:exported="true">
        <intent-filter>
            <action android:name="com.tutorialspoint.CUSTOM_INTENT> 
           </action>
        </intent-filter>
</receiver>

Still you'll have to find the real main activity that you're using and make sure it has the same as in Manifest.xml and in this case that is MainActivity.

<activity
    android:name=".MainActivity"
    android:exported="true"
    tools:ignore="Instantiatable">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

Upvotes: 2

Related Questions