ClassNotFoundException after changing app's name in Android
I have an app with main class called com.quarlityaq.quarlityaqcall.QuarlityaqCall
.
After finishing programming I've changed the app name to "JCall for jobs"
by changing app_name
under strings.xml
to "JCall for jobs"
android:label="@string/app_name" 开发者_JAVA技巧
android:name="@string/app_name"
On my device it works just fine, but on some other devices it crashes and throws:
java.lang.RuntimeException: Unable to instantiate application com.quarlityaqcall.QuarlityaqCall.JCall for jobs:
java.lang.ClassNotFoundException: com.quarlityaqcall.QuarlityaqCall.JCall for jobs
in loader dalvik.system.PathClassLoader[/data/app-private/com.quarlityaqcall.QuarlityaqCall-1.apk]
Caused by: java.lang.ClassNotFoundException:
com.quarlityaqcall.QuarlityaqCall.JCall for jobs in loader
dalvik.system.PathClassLoader[/data/app-private/com.quarlityaqcall.QuarlityaqCall-1.apk]
I don't understand why it looks for the class name "JCall for jobs" instead of "QuarlityaqCall", and why it only happens on some devices?
Application name in manifest refers to your Application class. If you have not implemented your own Application class you should not have name attribute in in your manifest file. If you have implemented one ou should make sure that the attribute refers to correct class.
You should not export the name attribute to strings.xml as it is a technical parameter for your app. Label is the one that is visible to users.
This error can also be caused by a call to getApplicationContext() (or similar exercise that requires this context, such as connecting an object via getSharedPreferences) within the application class' declaration method.
Move these calls to an override of the 'onCreate' method (or something similar). You get a null pointer to the application context if you call for it before the application context has been fully instantiated - by moving this code to a method/function 'deeper' in the 'instantiation stack' you will be accessing a non-null context (one that has been fully instantiated).
精彩评论