ClassNotFoundException - 无法实例化BroadcastReceiver

时间:2015-04-05 15:32:53

标签: android apk classnotfoundexception dex

我有一个Broadcastreceiver来检查CONNECTIVITY_CHANGE,它有时会与消息崩溃:

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会做吐司。

清单:

    <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;
}

任何导致崩溃的想法?感谢。

1 个答案:

答案 0 :(得分:3)

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

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

1。我正在使用具有启用ART的Kitkat的物理设备(即非仿真器)。当我在使用Dalvik运行时的另一台Kitkat设备上运行应用程序时,错误似乎已经消失。

2。由于我将SVN转移到其他位置时出现.dex构建错误,导致出现同样的问题。我完全删除了旧项目,并在我的计算机上从原始项目创建了一个新的存储库,完全重建项目后,错误没有重新出现。

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

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