Reputation: 1209
I have a class that i want to make a global class. The Global class have variables that i want to initialize in Main Activity class. Then I want to get the value of variables from Global class and set it to show in Textview of Second Activity Class. I have coded it to but i am getting an error. don't know what is wrong with it. I will be thankful to the person who will try to give the solution to it.
This is the GlobalClass.java
import android.util.Log;
public class GlobalClass {
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
This is the MainActivity.java Class. In this Class i want to initialize the variables of GlobalClass.java. However I am getting error int this line "final GlobalClass test=(GlobalClass)getApplicationContext();" and the error says "Can not cast from Context to GlobalClass"
public class MainActivity extends Activity {
Button btn;
Context context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
final GlobalClass test=(GlobalClass)getApplicationContext();//
globalVariable.setName("THis is me");
globalVariable.setEmail("sbd@somewhere");
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(),SecondScreen.class);
startActivity(intent);
}
});
}
}
This is my SecondScreen.java Class. I want to Get the values of variables in GLobalClass.java in this class.
public class SecondScreen extends Activity {
TextView tv;
Button btnButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_screen);
tv= (TextView) findViewById(R.id.tv);
btnButton=(Button) findViewById(R.id.thirdBtn);
final GlobalClass globalVariable=(GlobalClass)getApplicationContext();
final String name= globalVariable.getName();
final String email= globalVariable.getEmail();
String print= "Name: "+name+" Email:"+email;
tv.setText(print);
btnButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent= new Intent(getBaseContext(),ThirdScreen.class);
startActivity(intent);
}
});
}
}
This is my Manifest Class
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arifhaq.testexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:name="com.arifhaq.testexample.GlobalClass"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.arifhaq.testexample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.arifhaq.testexample.SecondScreen"
android:label="@string/title_activity_second_screen" >
</activity>
<activity
android:name="com.arifhaq.testexample.ThirdScreen"
android:label="@string/title_activity_third_screen" >
</activity>
</application>
</manifest>
Upvotes: 0
Views: 2275
Reputation: 463
Global class needs to extends application. You could do something like the following.
public class GlobalClass extends Application {
public static GlobalClass instance;
public void onCreate() {
instance = getApplication();
}
}
Upvotes: 1
Reputation: 622
You should let your global class extend Application.
An extensive answer can be found here:
How to declare global variables in Android?
Upvotes: 0
Reputation: 334
Usually you initialize your variables and stuff on Start of your Application to make sure u dont use uninitialized variables. You can do that in androit with onCreate() http://developer.android.com/reference/android/app/Application.html#onCreate()
Upvotes: 0