简体   繁体   English

如何在另一个活动中调用方法

[英]how to call method in another activity

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)

In short: you shouldn't. 简而言之:您不应该。 Make a separate (singleton) class which handles the kind() logic which both activities can access. 制作一个单独的(单例)类,该类处理两种活动都可以访问的kind()逻辑。

If you have code that needs to be shared between activities, you should export it to a Helper class. 如果您有需要在活动之间共享的代码,则应将其导出到Helper类。

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. 现在,您可以在两个活动中调用KindUtils.Kind(this)

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 始终确保类名应以大写字母开头,其java类命名约定
  2. Always use camel casing for methods name, starts with small caps. 始终使用骆驼套作为方法名称,从小写字母开始。
  3. You can't use keyword new for class that extend Activity. 您不能将关键字new用于扩展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. 这就是您从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 希望对你有帮助

First of all the looper error happens when you call a UI (user interface) method from a non UI thread. 首先,当您从非UI线程调用UI(用户界面)方法时,会发生弯针错误。 In this case because the second activity was not started as an intent, you started it as new firstClass() bad things will happen. 在这种情况下,因为第二个活动不是出于意图而启动的,所以您将其启动是因为新的firstClass()会发生不好的事情。 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. onCreate可以将引用传递给应该绘制的项目,通常至少是对布局setContentView(R.layout.activity_main);的引用,onStart是开始绘制。 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. 请记住,Android支持多种屏幕尺寸,并且必须询问您要添加到活动中的内容,多少,Android度量和数字如何显示,然后调用onstart来显示它,等等。 The main thing is start Activitys as intents and read the link. 最主要的是将Activity作为Intent启动并阅读链接。 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. 一种启动活动的特殊方式。

You simply don't instantiate instances of Activities in Android like you would do in Java. 您只需不像在Java中那样实例化Android中的Activities实例。 You should create Intents and call startActivity() . 您应该创建Intents并调用startActivity() To make your logic work, I suggest that you use BroadcastReceivers . 为了使您的逻辑起作用,我建议您使用BroadcastReceivers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM