问题描述
即时得到一个ClassCastException是不应该发生。
这里是code:
TwitterApplication TA =(TwitterApplication)getApplication();
OAuthProvider p值= ta.getProvider();
正如你看到我尝试从应用程序中投TwitterApplication。
公共类TwitterApplication扩展应用{私人Twitter的微博;公共Twitter的getTwitter(){
返回this.twitter;
}公共无效setTwitter(微博T){
this.twitter = T;
}私人OAuthProvider供应商;
私人消费CommonsHttpOAuthConsumer;公共无效setProvider(OAuthProvider OAP){
this.provider = OAP;
}公共OAuthProvider getProvider(){
返回this.provider;
}公共无效setConsumer(CommonsHttpOAuthConsumer C){
this.consumer = C;
}公共CommonsHttpOAuthConsumer getConsumer(){
返回this.consumer;
}
正如你在这里看到,它扩展应用程序。
是我的铸造知识有缺陷?
为什么会出现这个错误?
谢谢
编辑:
全堆栈跟踪:
15 03-03:45:09.275:ERROR / AndroidRuntime(3192):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.laytproducts.IN/com.laytproducts。 IN.TwitterUpdate}:java.lang.ClassCastException:android.app.Application
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在android.app.ActivityThread.access $ 1500(ActivityThread.java:124)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:968)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在android.os.Handler.dispatchMessage(Handler.java:99)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在android.os.Looper.loop(Looper.java:130)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在android.app.ActivityThread.main(ActivityThread.java:3806)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在java.lang.reflect.Method.invokeNative(本机方法)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在java.lang.reflect.Method.invoke(Method.java:507)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在dalvik.system.NativeStart.main(本机方法)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):java.lang.ClassCastException:产生的原因android.app.Application
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在com.laytproducts.IN.TwitterUpdate.getConsumerProvider(TwitterUpdate.java:148)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在com.laytproducts.IN.TwitterUpdate.onCreate(TwitterUpdate.java:117)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
03-03 15:45:09.275:ERROR / AndroidRuntime(3192):11 ...更多
清单:
<使用许可权的android:NAME =android.permission.INTERNET对/>
<使用许可权的android:NAME =android.permission.GET_ACCOUNTS>< /使用许可权><应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME>
<活动机器人:名字=。MainAct
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/>
<类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT; <活动机器人:主NAME = /> <活动机器人:名字=。FBUpdate>
&所述;意图滤光器>
<作用机器人:名字=com.laytproducts.IN.WidgetAct.ACTION_WIDGET_FB/>
&所述; /意图滤光器>
< /活性GT;
<活动机器人:名字=。TwitterUpdate>
&所述;意图滤光器>
<作用机器人:名字=com.laytproducts.IN.WidgetAct.ACTION_WIDGET_TWITTER/>
&所述; /意图滤光器>
< /活性GT; <应用机器人:名字=TwitterApplication/> <接收机器人:名字=。WidgetAct>
&所述;意图滤光器>
<作用机器人:名字=android.appwidget.action.APPWIDGET_UPDATE/>
&所述; /意图滤光器>
<元数据机器人:名字=android.appwidget.provider
机器人:资源=@ XML / winfo/>
< /接收器>
< /用途>
您会得到一个错误,如果通过getApplication()返回的具体类实际上不是TwitterApplication对象。
您已经修改了你的Android清单文件,以便它使用这个TwitterApplication类?如果没有,你需要做的。简单地扩展应用是不够的 - 你必须告诉Android的使用TwitterApplication具体类
Im getting a ClassCastException that shouldnt be happening.Here is the code:
TwitterApplication ta = (TwitterApplication)getApplication();
OAuthProvider p = ta.getProvider();
As you see im trying to cast TwitterApplication from an Application.
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;
}
As you see here, it extends application.
Is my casting knowledge faulty?Why am I getting this error?Thanks
EDIT:Full Stacktrace:
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
Manifest:
<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>
You will get an error if the concrete class returned by getApplication() is not actually a TwitterApplication object.
Have you amended your Android manifest file so that it uses this TwitterApplication class? If not, you will need to do. Simply extending Application is not enough - you must tell Android to use the TwitterApplication concrete class.
这篇关于ClassCastException异常不应该发生的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!