2012-03-03 46 views
1

我得到一個不應該發生的ClassCastException。 下面是代碼:ClassCastException不應該發生

TwitterApplication ta = (TwitterApplication)getApplication(); 
    OAuthProvider p = ta.getProvider(); 

正如你看到的我嘗試從應用程序中投TwitterApplication。

public class TwitterApplication extends Application { 

private Twitter twitter; 

public Twitter getTwitter(){ 
    return this.twitter; 
} 

public void setTwitter(Twitter t){ 
    this.twitter = t; 
} 

private OAuthProvider provider; 
private CommonsHttpOAuthConsumer consumer; 

public void setProvider(OAuthProvider oap){ 
    this.provider = oap; 
} 

public OAuthProvider getProvider(){ 
    return this.provider; 
} 

public void setConsumer(CommonsHttpOAuthConsumer c){ 
    this.consumer = c; 
} 

public CommonsHttpOAuthConsumer getConsumer(){ 
    return this.consumer; 
} 

正如您在此處看到的,它擴展了應用程序。

我的鑄造知識是否有缺陷? 爲什麼我得到這個錯誤? 感謝

編輯: 完整堆棧跟蹤:

03-03 15:45:09.275: ERROR/AndroidRuntime(3192): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.laytproducts.IN/com.laytproducts.IN.TwitterUpdate}: java.lang.ClassCastException: android.app.Application 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at android.app.ActivityThread.access$1500(ActivityThread.java:124) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at android.os.Looper.loop(Looper.java:130) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at android.app.ActivityThread.main(ActivityThread.java:3806) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at java.lang.reflect.Method.invoke(Method.java:507) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at dalvik.system.NativeStart.main(Native Method) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192): Caused by: java.lang.ClassCastException: android.app.Application 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at com.laytproducts.IN.TwitterUpdate.getConsumerProvider(TwitterUpdate.java:148) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at com.laytproducts.IN.TwitterUpdate.onCreate(TwitterUpdate.java:117) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660) 
03-03 15:45:09.275: ERROR/AndroidRuntime(3192):  ... 11 more 

清單:

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission> 

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".MainAct" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity android:name=".Main" /> 

    <activity android:name=".FBUpdate"> 
     <intent-filter> 
      <action android:name="com.laytproducts.IN.WidgetAct.ACTION_WIDGET_FB" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".TwitterUpdate"> 
     <intent-filter> 
      <action android:name="com.laytproducts.IN.WidgetAct.ACTION_WIDGET_TWITTER" /> 
     </intent-filter> 
    </activity> 

    <application android:name="TwitterApplication" /> 

    <receiver android:name=".WidgetAct"> 
     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
     </intent-filter> 
     <meta-data android:name="android.appwidget.provider" 
      android:resource="@xml/winfo"/> 
    </receiver> 
</application> 
+4

是什麼'getApplication'返回? – 2012-03-03 23:21:34

+0

什麼是完整的堆棧跟蹤? – Jeffrey 2012-03-03 23:25:05

+0

getApplication返回一個應用程序。 我的帖子在編輯中已滿。 – Brandon 2012-03-03 23:45:40

回答

3

,你會得到一個錯誤,如果通過getApplication()返回的具體類實際上不是TwitterApplication對象。

您是否修改了Android清單文件,以便它使用此TwitterApplication類?如果沒有,你需要這樣做。簡單地擴展應用程序是不夠的 - 你必須告訴Android使用TwitterApplication具體類。

+0

我會怎麼做?我以前從未使用過應用程序。 – Brandon 2012-03-04 00:17:25

+0

沒關係。發現。不得不放入 thingy。不是做一個新的。謝謝隊友 – Brandon 2012-03-04 00:19:59

2

在你的清單文件,請確保您有:

<application android:name="TwitterApplication"> 
+0

沒有工作。你可以檢查我的編輯看到我的完整清單(或大部分完整)。 – Brandon 2012-03-03 23:57:44

+0

當然,您需要編輯現有的'application'標記,而不是創建一個新標記。 – Macondo2Seattle 2012-03-04 01:07:37

+0

在我發佈該評論XD後不久,Ya發現了這一點 – Brandon 2012-03-04 02:44:08

相關問題