java - 不应发生的 ClassCastException

标签 java android class 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

list :

 <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>

最佳答案

如果 getApplication() 返回的具体类实际上不是 TwitterApplication 对象,您将得到一个错误。

您是否修改了您的 Android list 文件以使其使用此 TwitterApplication 类?如果没有,您将需要做。仅仅扩展 Application 是不够的 - 您必须告诉 Android 使用 TwitterApplication 具体类。

关于java - 不应发生的 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9550745/

相关文章:

java - 每次我在 Eclipse 上重新启动服务器时,Eclipse Tomcat 服务器配置都会重置

java - Spring MVC 应用程序中的多个 ScriptTemplateViewResolvers

android - 如何保持 session

Python 减去 float

class - 如何在两个文件中拥有一个类和父类(super class)(Typescript)

c++ - 成员变量在 C++ 中因未知原因而更改..?

java - {{notification.header}} {{notification.message}} 在访问 Keycloak 管理控制台时加载

java - 如何使用 xpath 获取遵循特定部分中的模式的链接?

Android Studio - 为所有模块添加依赖

android - 如何更改SD卡默认保存文件夹?