最佳答案
我有一个定制的全局类,它看起来像这样
import android.app.Application;
public class MyApp extends Application {
public String MainAct;
public String getMainAct() {
return MainAct;
}
public void setMainAct(String mainAct) {
MainAct = mainAct;
}
}
我想通过 onCreate
方法中的另一个 Activity
来保留这个类中的字符串。
String local = "myLocalVariable";
((MyApp) getApplication()).setMainAct(local); //breaks here!!!
String name = ((MyApp) getApplication()).getMainAct();
它在标记的行上中断,并出现错误: Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.xxx.yyy.global.MyApp
我已经检查代码五次了,我找不到任何错误。有人能告诉我错误在哪里吗!
谢谢