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

查看:120
本文介绍了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)

接收器的目的很简单-互联网断开后,jsut会吐司.

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..由于我将SVN移至另一个位置时发生了.dex构建错误,因此出现了相同的问题.我完全删除了旧项目,并从计算机上的原始项目中创建了一个新的存储库,并且在完全重建该项目之后,该错误不再出现.

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天全站免登陆