tags:

views:

81

answers:

2

I am trying to setup and global variable, but I my app fails after adding the following line to my AndroidManifest.xml

<application android:name=".MyApp"
              android:icon="@drawable/icon" 
              android:label="@string/app_name">

I am using the following code as well:

Class: package com.mynamecompany.datahelp;

import android.app.Application;

class MyApp extends Application {

      private String myState;

      public String getState(){
        return myState;
      }
      public void setState(String s){
        myState = s;
      }
    }

Usage:

MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
Toast.makeText(getApplicationContext(), "My Value-" + state, Toast.LENGTH_SHORT).show();
appState.setState("Test");
Toast.makeText(getApplicationContext(), "My Value-" + appState.getState(), Toast.LENGTH_SHORT).show();

The program starts and errors immediately on the Splash screen before the usage code can be called further in the program, on a different Activity.

Any ideas?

+2  A: 

You are trying to cast to your class from Context. You have to call getApplication.

Janusz
I changed getApplicationContext to just getApplication, but the application blows up as soon as I start it still because of the line to manifest.xml:<application android:name=".MyApp"
Is datahelp defined in the package part of your manifest? You have to specify the path to your application class in the manifest in the same way you are specifying it for activities
Janusz
+1  A: 

What's the package indication in your manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mynamecompany">
    <application android:name=".datahelp.MyApp"
        android:icon="@drawable/icon" android:label="@string/app_name">
...

Probably you have to change it as I made it above. The rest should just work fine, tried it out on my own project.

Juri
still a no go, something I must doing wrong. I do call a thread on the splash screen, would that cause an issue?Dean
Potentially it could. Do you have the possibility to post some more code. It's quite difficult to judge and spot the problem with the amount you provide right now :)
Juri