java.lang.RuntimeException on Android: Unable to instantiate receiver

Asked 2 years ago, Updated 2 years ago, 88 views

While implementing on Android, the following exceptions occur:

FATAL EXCEPTION:main
Process: "Application Name", PID: 19962
java.lang.RuntimeException: Unable to instantiate receiver "ClassA": java.lang.InstantiationException: can't instantiate class "ClassA"
    at android.app.ActivityThread.handleReceiver (ActivityThread.java:2441)
    at android.app.ActivityThread.access$1700 (ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1322)
    at android.os.Handler.dispatchMessage (Handler.java:102)
    at android.os.Looper.loop (Looper.java:212)
    at android.app.ActivityThread.main (ActivityThread.java:5137)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke (Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:902)
    at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:718)
    at dalvik.system.NativeStart.main (Native Method)
Caused by: java.lang.InstantiationException: can't instantiate class "ClassA"
    at java.lang.Class.newInstanceImpl (Native Method)
    at java.lang.Class.newInstance (Class.java:1208)
    at android.app.ActivityThread.handleReceiver (ActivityThread.java:2436)
    ... 10 more

Source Code
BroadcastReceiver

public abstract class ClassAextends BroadcastReceiver{

    public ClassA(){
        super();
    }

    @ Override
    public final void onReceive (final Context context, final Intent) {
        String actionName = intent.getAction();
        if(actionName.equals("A"){
        }
    }
}

Source

public static void sendBroadcast(final Context context, Serializable obj){

        Intent = new Intent();
        int.setAction("A");
        int.putExtra("key", obj);

        context.sendBroadcast(intent);
}

Manifest

<receiver android:name=".ClassA">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACED"/>
        <data android:scheme="package"/>
    </intent-filter>
    <intent-filter>
        <action android:name="A"/>
    </intent-filter>
</receiver>

I am sending a class that implements Serializable to Intent with putExtra.
My device uses Android 4.4.4.

Could you tell me how to deal with it?
Thank you for your cooperation.

android java

2022-09-30 20:50

1 Answers

The class declaration is an abstract class.

As a basic Java story, abstract classes cannot be instantiated.

public class ClassAextends BroadcastReceiver

If there is no need to abstract, the class declaration should be as shown above.


2022-09-30 20:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.