我有一个名为RiddlesHolder
的课程:
public class RiddlesHolder extends Application {
ArrayList<String> funnyRiddles = new ArrayList<String>();
ArrayList<String> funnyRiddlesAnswers = new ArrayList<String>();
ArrayList<String> KidsRiddles = new ArrayList<String>();
ArrayList<String> KidsRiddlesAnswers = new ArrayList<String>();
ArrayList<String> AnimalRiddles = new ArrayList<String>();
ArrayList<String> AnimalRiddlesAnswers = new ArrayList<String>();
public void addAnimalRiddles(){
AnimalRiddles.add("1");
AnimalRiddles.add("2");
}
public void addAnimalRiddlesAnswers(){
AnimalRiddlesAnswers.add("1");
AnimalRiddlesAnswers.add("2");
}
public void addFunnyRiddles(){
funnyRiddles.add("1");
funnyRiddles.add("2");
}
public void addFunnyRiddleAnswers(){
funnyRiddlesAnswers.add("1");
funnyRiddlesAnswers.add("2");
}
public void adKidsRiddles(){
KidsRiddles.add("1");
KidsRiddles.add("2");
}
public void addKidsRiddlesASnswers(){
KidsRiddlesAnswers.add("1");
KidsRiddlesAnswers.add("2");
}
public ArrayList<String> getAnimalRiddles() {
return AnimalRiddles;
}
public ArrayList<String> getAnimalRiddlesAnswers() {
return AnimalRiddlesAnswers;
}
public ArrayList<String> getFunnyRiddles() {
return funnyRiddles;
}
public ArrayList<String> getFunnyRiddlesAnswers() {
return funnyRiddlesAnswers;
}
public int getFRiddleByContent(String content) {
return funnyRiddles.indexOf(content);
}
public ArrayList<String> getKidsRiddles() {
return KidsRiddles;
}
public ArrayList<String> getKidsRiddlesAnswers() {
return KidsRiddlesAnswers;
}
public int getKidsRiddleByContent(String content) {
return KidsRiddles.indexOf(content);
}
}
此活动的onCreateView
内的这一行:
final RiddlesHolder riddles = (RiddlesHolder) getActivity().getApplication();
并抛出以下错误:
java.lang.ClassCastException: android.app.Application cannot be cast to com.tc.tools.RiddlesHolder
at com.tc.gatanki.FunnyRiddles$PlaceholderFragment.onCreateView(FunnyRiddles.java:104)
我在这里缺少什么?我有点困惑,因为这是工作代码。我只是从eclipse转移到由intelliJ驱动的新的android工作室(谷歌建议,即使它仍处于BETA阶段)。为什么我的代码中出现此错误?我知道我在这里错过了一小部分,但是我无法在妈妈那里发现它。
答案 0 :(得分:2)
自定义应用程序注入由AndroidManifest.xml驱动,因此如果不是您的应用程序类被实例化,那可能是由于AndroidManifest.xml不正确。你能检查它在Android Studio中的样子,甚至是两个IDE生成的APK吗? <application>
标记上应该有一个属性,例如android:name="om.tc.tools.RiddlesHolder"
。