自全球应用课间与" android.app.Application不能转换到" [英] Custom global Application class breaks with "android.app.Application cannot be cast to"
问题描述
我有一个看起来像这样一个自定义的全局类
I have a custom global class which looks like this
import android.app.Application;
public class MyApp extends Application {
public String MainAct;
public String getMainAct() {
return MainAct;
}
public void setMainAct(String mainAct) {
MainAct = mainAct;
}
}
我想preserve通过另一个活动
在的onCreate
方法的字符串在这个类。
I want to preserve a string in this class via another Activity
in its onCreate
method.
String local = "myLocalVariable";
((MyApp) getApplication()).setMainAct(local); //breaks here!!!
String name = ((MyApp) getApplication()).getMainAct();
它打破了与错误的标记线:致:java.lang.ClassCastException:android.app.Application不能转换为com.xxx.yyy.global.MyApp
我检查了code五次,我无法找到错误的任何地方。谁能告诉我在哪里,错误的是?!
I've checked the code for five times and I cannot find error anywhere. Can anyone tell me where the error is?!
感谢
推荐答案
该错误提示按 getApplication
返回的对象的类型是安卓.app.Application
。一个可能的原因是,你无法定义清单中的应用程序。请确保您的清单包括一些在该行:
The error states that the type of the object returned by getApplication
is android.app.Application
. A possible cause for this is that you failed to define the application in the manifest. Make sure that your manifest includes something in the lines of:
<application android:name=".MyApp"...
</application>
这篇关于自全球应用课间与&QUOT; android.app.Application不能转换到&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!