ClassNotFoundException - 无法实例化 BroadcastReceiver [英] ClassNotFoundException - Unable to instantiate BroadcastReceiver

查看:24
本文介绍了ClassNotFoundException - 无法实例化 BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Broadcastreceiver 来检查 CONNECTIVITY_CHANGE 并且它有时会崩溃并显示消息:

I have a Broadcastreceiver which checks for CONNECTIVITY_CHANGE and it crashes sometimes with message:

04-05 18:23:47.080    5561-5561/tenkol.design.com.imbrecords E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate receiver tenkol.design.com.imbrecords.NetworkChangeReceiver: java.lang.ClassNotFoundException: tenkol.design.com.imbrecords.NetworkChangeReceiver
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2541)
        at android.app.ActivityThread.access$1500(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5520)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassNotFoundException: tenkol.design.com.imbrecords.NetworkChangeReceiver
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2536)
            at android.app.ActivityThread.access$1500(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5520)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
            at dalvik.system.NativeStart.main(Native Method)

接收器的目的很简单 - 只需在互联网连接消失时制作吐司.

The aim of receiver is simple - jsut make toast when internet connection is gone.

清单:

    <receiver
        android:name=".NetworkChangeReceiver"
        android:label="NetworkChangeReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>    

和接收者本身:

public class NetworkChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, final Intent intent) {

    String status = InternetConnection.getConnectivityStatusString(context);

    if (status != null) {
        if (status.equals("Not connected to Internet")) {
            Toast.makeText(context, context.getString(R.string.lost_internet_connection), Toast.LENGTH_SHORT).show();
        }
    }
}

这些是它使用的方法:

public static int getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return TYPE_WIFI;

        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return TYPE_MOBILE;
    }
    return TYPE_NOT_CONNECTED;
}

public static String getConnectivityStatusString(Context context) {
    int conn = InternetConnection.getConnectivityStatus(context);
    String status = null;
    if (conn == InternetConnection.TYPE_NOT_CONNECTED) {
        status = "Not connected to Internet";
    }
    return status;
}

任何想法导致崩溃?谢谢.

Any ideas what causes crashes? Thanks.

推荐答案

ClassNotFoundException 表示您的应用的 APK 未正确构建.这似乎是一个 dex 构建错误;这里的线索是异常是由 BaseDexClassLoader.findClass() 方法抛出的.

The ClassNotFoundException is an indication that your app's APK has not been built correctly. It seems to be a dex build error; the clue here is that the exception is thrown by the BaseDexClassLoader.findClass() method.

我之前在以下情况下观察到此错误:

I have observed this error in the following situations previously:

1. 我使用的是启用了 ART 的 Kitkat 的物理设备(即不是模拟器).当我在另一个带有 Dalvik 运行时的 Kitkat 设备上运行该应用程序时,错误似乎消失了.

1. I was using a physical device (i.e. not an emulator) that had Kitkat with ART enabled. When I ran the app on another Kitkat device with Dalvik runtime, the error seemed to have vanished.

2. 由于 .dex 构建错误,当我将 SVN 移动到另一个位置时发生了同样的问题.我完全删除了旧项目并在我的计算机上从原始项目创建了一个新存储库,并且在完全重建项目后错误没有再次出现.

2. The same problem arose due to a .dex build error which occurred when I shifted the SVN to another location. I deleted the old project entirely and created a new repository from the original project on my computer, and after rebuilding the project entirely the error did not reappear.

3. 如果将 Eclipse 与 Maven 结合使用,并且您已启用 multidex,请尝试禁用它并构建项目.如果使用 Android Studio,请设置 multiDexEnabled = false.这是此错误的已知原因.

3. If using Eclipse with Maven, and you have enabled multidex, then try disabling it and building the project. If using Android Studio, set multiDexEnabled = false. This is a known cause of this error.

基本上你需要正确重建项目,错误就会消失.

Basically you need to rebuild the project correctly and the error will go away.

这篇关于ClassNotFoundException - 无法实例化 BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆