zoukankan      html  css  js  c++  java
  • android自己定义Application全局变量不能类型转换的问题

    今天弄了个全局变量AppContext ,但一直出现例如以下错误,原来继承 Application的得在清单文件声明。
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.langteng.shiliao/com.langteng.shiliao.activity.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.langteng.shiliao.until.AppContext
    public class AppContext extends Application {
        public static final int NETTYPE_WIFI = 0x01;
        public static final int NETTYPE_CMWAP = 0x02;
        public static final int NETTYPE_CMNET = 0x03;
        public static final int PAGE_SIZE = 20;//默认分页大小
        private static final int CACHE_TIME = 10*60000;//缓存失效时间
    .................................
    然后在activity中通过getApplication()获得一个Application类型的变量,并转换成自己定义的AppContext。例如以下: 
    appContext = (AppContext) getApplication();
    结果在执行的时候报类型转换错误。
    后来在网上差了好久才发现自己定义的全局Application必须在AndroidManifest.xml中注冊声明:
    <application
        android:name=".AppContext"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

  • 相关阅读:
    第07组 Alpha冲刺 (2/6)
    第07组Alpha冲刺(1/6)
    第四次作业
    面试题练习
    SpringMVC访问静态资源
    MyBatis基础
    Spring注解和jdk注解
    自动代理生成器
    aspect xml
    Spring-案例
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6906040.html
Copyright © 2011-2022 走看看