標籤:launcher app android 全域變數
今天弄了個全域變數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" >
android自訂Application全域變數不能類型轉換的問題