I am trying to put my list of custom objects in the application class to access my data in multiple activities. But something doesn’t work, and I don’t know what’s going wrong.
This is what I’m doing:
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("myJsonFile.json");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
ObjectMapper objectMapper = new ObjectMapper();
try {
List<JJsonResponse> jsonResponse = objectMapper.readValue(inputStream, new TypeReference<List<JJsonResponse>>() { });
Log.i("tijdlog","einde parsing" );
final List<JJsonResponse> myGlobalVariable = jsonResponse;
((ApplicationController)getApplication()).setGlobalVariable(myGlobalVariable);
where JJsonResponse is some custom object. Without the last part, everything worked just fine.
My ApplicationController class:
public class ApplicationController extends Application {
private List<JJsonResponse> venueController;
public List<JJsonResponse> getGlobalVariable() {
return venueController;
}
public void setGlobalVariable(List<JJsonResponse> globalVariable) {
this.venueController = globalVariable;
}
@Override
public void onCreate() {
}
}
when running the program crashes.
error log:
07-20 14:06:09.487: W/dalvikvm(6668): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
07-20 14:06:09.497: E/AndroidRuntime(6668): FATAL EXCEPTION: main
07-20 14:06:09.497: E/AndroidRuntime(6668): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jacksonrecipes.testapp/com.jacksonrecipes.testapp.Main}: java.lang.ClassCastException: android.app.Application
07-20 14:06:09.497: E/AndroidRuntime(6668): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
07-20 14:06:09.497: E/AndroidRuntime(6668): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
07-20 14:06:09.497: E/AndroidRuntime(6668): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
07-20 14:06:09.497: E/AndroidRuntime(6668): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
07-20 14:06:09.497: E/AndroidRuntime(6668): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 14:06:09.497: E/AndroidRuntime(6668): at android.os.Looper.loop(Looper.java:144)
07-20 14:06:09.497: E/AndroidRuntime(6668): at android.app.ActivityThread.main(ActivityThread.java:4937)
07-20 14:06:09.497: E/AndroidRuntime(6668): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 14:06:09.497: E/AndroidRuntime(6668): at java.lang.reflect.Method.invoke(Method.java:521)
07-20 14:06:09.497: E/AndroidRuntime(6668): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-20 14:06:09.497: E/AndroidRuntime(6668): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-20 14:06:09.497: E/AndroidRuntime(6668): at dalvik.system.NativeStart.main(Native Method)
07-20 14:06:09.497: E/AndroidRuntime(6668): Caused by: java.lang.ClassCastException: android.app.Application
07-20 14:06:09.497: E/AndroidRuntime(6668): at com.jacksonrecipes.testapp.Main.onCreate(Main.java:52)
07-20 14:06:09.497: E/AndroidRuntime(6668): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
07-20 14:06:09.497: E/AndroidRuntime(6668): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
I made the variable final cause I thought it might have something to do with that, but that didn’t work. Does anyone know what the uncaught exception might be and how to solve my problem?
edit: Forgot to show my manifest, I don’t know if that’s right so I added it:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<application
android:label="@string/app_name"
android:name=".ApplicationController"
/>
You should define the name of your Application in your Android project’s
Manifest.xmltoApplicationControllersomething like this:EDIT You have supplied two applications in your manifest. Change your Manifest from this:
to this:
And i think you will be ok