英文:
How to resolve ClassCastException: java.lang.String cannot be cast exception on android
问题
这是我的错误代码
public static MangJekApplication getInstance(Context context) {
    return (MangJekApplication) context.getApplicationContext();
}
这是我的错误日志
*java.lang.ClassCastException: android.app.Application无法转换为com.alexa.grajek1.MangJekApplication*
英文:
This is my error code
public static MangJekApplication getInstance(Context context) {
    return (MangJekApplication) context.getApplicationContext();
}
This is my error Log
*java.lang.ClassCastException: android.app.Application cannot be cast to com.alexa.grajek1.MangJekApplication*
答案1
得分: 1
首先,MangJekApplication 应该扩展 Application:
//其他内容
public class MangJekApplication extends Application {
    public static MangJekApplication getInstance(Context context) {
        return (MangJekApplication) context.getApplicationContext();
    }
}
//其他内容
其次,在 AnroidManifiest.xml 文件中添加 android:name,如下所示:
<application 
   android:name=".MangJekApplication"
   //其他内容
   ...>
英文:
Firstly, the MangJekApplication should extends Application:
//other stuffs
public class MangJekApplication extends Application {
    public static MangJekApplication getInstance(Context context) {
        return (MangJekApplication) context.getApplicationContext();
    }
}
//other stuffs
Secondly, you should add android:name in AnroidManifiest.xml file as following:
<application 
   android:name=".MangJekApplication"
   //other stuffs
   ...>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论