Reputation: 19
I should call a method from a activity to another activity. my firstclass is:
public class firstclass extends Activity {
public String Kind(){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean Key = preferences.getBoolean("Key", true);
if(Key){
name="you";
}
else{
name="me";
}
return name;
}
}
secondclass is:
public class secondclass extends Activity {
public void take(String token, int transactionId) {
firstclass first = new firstclass(); //error in this class
first.Kind();
}
}
My error is:
03-25 19:05:39.082 5421-5487/com.example.com E/AndroidRuntime﹕ FATAL EXCEPTION: pool-5-thread-1
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:197)
at android.os.Handler.<init>(Handler.java:111)
at android.app.Activity.<init>(Activity.java:759)
at com.example.com.firstclass<init>(firstclass.java:17)
at com.example.com.secondclass(secondclass.java:157)
Upvotes: 0
Views: 7769
Reputation: 4179
You simply don't instantiate instances of Activities
in Android like you would do in Java. You should create Intents
and call startActivity()
. To make your logic work, I suggest that you use BroadcastReceivers
.
Upvotes: 0
Reputation: 696
First of all the looper error happens when you call a UI (user interface) method from a non UI thread. In this case because the second activity was not started as an intent, you started it as new firstClass() bad things will happen.Activitys have lifecycle callbacks. onCreate to pass refrences to what items should be drawn usually at least a refrence to a layout setContentView(R.layout.activity_main);, onStart is to start drawing. Keep in mind Android supports many screen sizes and must ask what you want put into the activity, how many, android measures and figures how to display then calls onstart to display it and so on. The main thing is start Activitys as intents and read the link. In the manifest Activity block you have Launcher which means put an icon on the display to start an Activity when the icon is clicked. A special way of starting an activity as an intent.
Upvotes: 0
Reputation: 7749
If you have code that needs to be shared between activities, you should export it to a Helper class.
Example:
public class KindUtils {
public static String Kind(Context context){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean Key = preferences.getBoolean("Key", true);
if(Key){
name="you";
}
else{
name="me";
}
return name;
}
}
Now you can call KindUtils.Kind(this)
in both activites.
Upvotes: 1
Reputation: 7104
That's not how you do it. Let me correct you code as per my knowledge.
new
for class that extend Activity.Now how can you access the method of other activity from some activity
((Firstclass)getActivity()).kind;
That's how you access the method from other class in android.
public class Firstclass extends Activity {
public String kind(){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean Key = preferences.getBoolean("Key", true);
if(Key){
name="you";
}
else{
name="me";
}
return name;
}
}
in Second Class you'll do
public class Secondclass extends Activity {
public void take(String token, int transactionId) {
String str= ((Firstclass)getActivity()).kind;
}
}
Hope it will help
Upvotes: 0
Reputation: 2950
In short: you shouldn't. Make a separate (singleton) class which handles the kind()
logic which both activities can access.
Upvotes: 0