Reputation: 744
I have this class named 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);
}
}
and this line inside of the activity's onCreateView
:
final RiddlesHolder riddles = (RiddlesHolder) getActivity().getApplication();
and it throws the following error:
java.lang.ClassCastException: android.app.Application cannot be cast to com.tc.tools.RiddlesHolder
at com.tc.gatanki.FunnyRiddles$PlaceholderFragment.onCreateView(FunnyRiddles.java:104)
What am I missing here? I'm a bit confused, because this is working code. I'm just moving from eclipse to the new android studio powered by intelliJ(suggested by google even if it is still in a BETA stage). Why am I getting this error in my code? I know that I'm missing an extremely small part here, but I'm not able to spot it at the momment.
Upvotes: 0
Views: 98
Reputation: 1955
The custom Application injection is driven by the AndroidManifest.xml, so if it's not your application class being instantiated, that could be due to the AndroidManifest.xml being incorrect. Can you check how it looks like in Android Studio, or even in the APKs that the two IDEs generated? There should be an attribute on the <application>
tag something like android:name="om.tc.tools.RiddlesHolder"
.
Upvotes: 2