问题描述
我有一个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; }
有什么想法导致崩溃的原因?谢谢.
推荐答案
ClassNotFoundException表明您的应用程序的APK尚未正确构建.似乎是dex构建错误;这里的线索是例外是由BaseDexClassLoader.findClass()方法抛出的.
我以前在以下情况下观察到了此错误:
1.我正在使用具有启用ART KITKAT的物理设备(即不是模拟器).当我使用Dalvik运行时运行该应用程序时,错误似乎已经消失了.
2.由于.dex构建误差而出现同样的问题,这是我将SVN转移到另一个位置时发生的.我完全删除了旧项目,并从我的计算机上的原始项目创建了一个新的存储库,在重建项目后,错误并未重新出现.
3.如果使用Maven使用Eclipse,并且您已启用multidex,请尝试禁用它并构建项目.如果使用Android Studio,请设置multiDexEnabled = false.这是此错误的已知原因.
基本上您需要正确重建项目,并且错误将消失.
问题描述
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.
Manifest:
<receiver android:name=".NetworkChangeReceiver" android:label="NetworkChangeReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
and the receiver itself:
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(); } } }
These are the methods it uses:
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.
推荐答案
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. 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. 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. 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.