0

i want to share the global data across activities and i have followed

this link

.but i am not getting how to declare it in my manifest. i am posting my manifest code, i have tried it in different ways, but still getting the error. please tell me how to resolve it.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.helloandroid"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name"
     android:name=".Myapp">
    <activity android:name=".AndroidtestActivity"
              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>

This is my main activity

public class AndroidtestActivity extends Activity
{

    /** Called when the activity is first created. */


  public static final String PREFS_NAME = "MyPrefsFile";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditText et = (EditText) findViewById(R.id.text1);

    Myapp appState = ((Myapp)getApplicationContext());

   String s= appState.getState();


    et.setText(s);


}

}

and my Myapp class is

class Myapp extends Application {

  private String myState;

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

i am getting error in the line Myapp appState = ((Myapp)getApplicationContext());

illegal access exception ,please tell me how to resolve this problem

please help me with this.

Community
  • 1
  • 1
siva
  • 167
  • 1
  • 12
  • What you really want to do, i want to share the global data across activities. Can you be more specific what you ant to share – Triode Mar 21 '12 at 16:06
  • i want to share a string variable across activities and for that i am using the above link, but still i am not getting how to declare it exactly. that is my problem – siva Mar 21 '12 at 16:23
  • What you want to do post your code, so that reply would be fast. – Triode Mar 21 '12 at 16:29
  • pleas someone help me with this. i need it ASAP – siva Mar 21 '12 at 17:50
  • I have found the mistake, it is my fault i have to use Public Myapp extends application , that i did not do it. – siva Mar 21 '12 at 18:26

1 Answers1

0

Merge the application tags (i.e. put the android:name in the first one and delete the rest):

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

    <activity android:name=".AndroidtestActivity"
              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>

Then use getApplication() instead of getApplicationContext().

Stefan
  • 4,645
  • 1
  • 19
  • 35
  • thanks for ur help stefan, but still i am getting illegal class access exception. how can i resolve it please tell me. please look at my edited post of manifest – siva Mar 21 '12 at 16:52
  • Myapp appState = ((Myapp)getApplication()); // NOT ... getApplicationContext()! – Stefan Mar 22 '12 at 07:23