0

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)
Tom
  • 19
  • 1
  • 8
  • Please paste Kind() method implementation code . – Shadab Ansari Mar 25 '16 at 19:51
  • To make use of some method of another class, you should make it "static", and then you have to call it typing "firstclass.Kind()" you don't have to initialize "firstclass" to call his methods – M. Mariscal Mar 25 '16 at 20:00
  • when make it "static" can not know "this" in: SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); – Tom Mar 25 '16 at 20:15
  • @Tom You are correct, you can't reference a non-static object, variable etc .. outside of a static method, unless they are also static, however you can't make 'this` (referencing itself) static. M. Mariscal was partially correct, but not in this particular case. why can't you just get the preference manager/shared preferences (clue's in the word 'shared') in your second class?? – Mark Mar 25 '16 at 20:19

5 Answers5

1

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.

F43nd1r
  • 7,690
  • 3
  • 24
  • 62
0

In short: you shouldn't. Make a separate (singleton) class which handles the kind() logic which both activities can access.

Roel Strolenberg
  • 2,922
  • 1
  • 15
  • 29
  • Just curious, if you pass a Activity context to a singleton class would there be a potential memory leak, as Activities can come and go? – Mark Mar 25 '16 at 20:37
  • First off: Android sdk handles destruction and construction of activities unless you mess with the core functionalities. Second: You don't need to pass the context of the activity to the singleton class, just a set of variables which will then handle the storing. This is explained here: http://stackoverflow.com/questions/2614719/how-do-i-get-the-sharedpreferences-from-a-preferenceactivity-in-android If you really need your preferences saved in a specific activity, you might want to rethink your general design =) – Roel Strolenberg Mar 26 '16 at 10:51
0

That's not how you do it. Let me correct you code as per my knowledge.

  1. Always make sure that class name should start with Capital letter its java class naming convention
  2. Always use camel casing for methods name, starts with small caps.
  3. You can't use keyword 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

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
  • no no no. This is how you read a field from an instance, but you already have to have an instance, otherwise it won't work. Also this asks for a method, not a field. You have a good point with the naming conventions though. – F43nd1r Mar 25 '16 at 20:02
0

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.

Pomagranite
  • 696
  • 5
  • 11
0

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.

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24