问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/29458927/classnotfoundexception-unable-to-instantiate-broadcastreceiver