简体   繁体   English

如何从活动中访问方法并将其用于Android中的另一个活动?

[英]How can I access a method from an activity and use it into another activity in Android?

I have the first class named iHave 我有第一堂课叫iHave

public class iHave extends ActionBarActivity
{   

//below is the instance for calling the method from the other activity. 
(The name of the other activity is **iThank**)

**iThank thankYou = new iThank();**

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_i_have);

    Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            **//this is the method I want to access from iThank class** **strong text**
            thankYou.display();
        }
    });       
} 

//The Next class is "iThank" //下一个类是“ iThank”

public class iThank extends ActionBarActivity
{

 @Override

protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_i_thank);

        txtThank = (TextView) findViewById(R.id.textView3);

    //this is the method I want to access/use from iHave Activity 
    public void display()
    {
        txtThank.setText ("Shine");
    }
}

How can I use the method " public void display() " of iThank activity to the "iHave" activity? 如何将iThank活动的“ public void display() ”方法用于“ iHave”活动? It always gives me an error of NullPointerException . 它总是给我一个NullPointerException错误。 Please help. 请帮忙。 Thank you very much! 非常感谢你!

How can I access a method from an activity and use it into another activity in Android? 如何从活动中访问方法并将其用于Android中的另一个活动?

By creating object for other to access method from Activity is right way. 通过创建对象以供其他人从Activity访问方法是正确的方法。

Use LocalBroadcastManager for communicating between application components. 使用LocalBroadcastManager在应用程序组件之间进行通信。

1. Send broadcast from iHave on Button click: 1.在按钮上单击,从iHave发送广播:

@Override
   public void onClick(View v)
     {
       Intent intent = new Intent("DISPLAY_EVENT");
       LocalBroadcastManager.getInstance(v.getContext()).sendBroadcast(intent);
     }

2. Register LocalBroadcastManager in iThank Activity: 2.iThank活动中注册LocalBroadcastManager

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(ReceiveMessage,
            new IntentFilter("DISPLAY_EVENT"));
}

3. Create BroadcastReceiver object and call display() method in iThank Activity: 3.iThank Activity中创建BroadcastReceiver对象并调用display()方法:

private BroadcastReceiver ReceiveMessage = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
             display();
        }
    };

Also add null check in display method for TextView: 还要在TextView的显示方法中添加null检查:

public void display()
{   
   if(txtThank !=null)
    txtThank.setText ("Shine");
}

Please don't to this, it is not how activities are intended to work. 请不要这样做,这不是活动的工作方式。 You might want to have a look over the Activities Developer Guide to get started. 您可能需要查看《 活动开发人员指南》以开始使用。 If you want to launch a new activity (eg iThank ) from the current foreground activity (eg iHave ), you never instantiate the class yourself directly and always launch it using an intent . 如果要从当前前台活动(例如iHave )启动新活动(例如iThank ),则永远不要自己直接实例化该类,而始终 使用intent启动它 If you have data to pass along (such as a message to display), it needs to be bundled along with the intent as an extra (see same link). 如果您有数据要传递(例如要显示的消息),则需要将其与意图捆绑在一起(请参阅同一链接)。

Activities should never call methods on each other directly, because this requires them to have references to each other. 活动永远不要直接在彼此之间调用方法,因为这要求它们之间具有相互引用。 The framework manages the life cycle of each activity independently, and those references can lead to leaks. 该框架独立管理每个活动的生命周期,这些引用可能导致泄漏。

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

相关问题 如何在另一个活动中使用一个活动中的方法? - How to use a method from one Activity in another Activity? 如何从另一个Android Activity调用Activity的公共方法 - How to call a public method of an Activity from another Android Activity 如何在Android应用程序中全局访问另一个班级的活动? - How can I access an Activity from another class globally in an Android app? 如何通过传递数据从另一个活动中调用方法? - How to I can call a method from another activity with passing data? 如何从另一个活动调用一个活动的方法? - How can you call a method of an activity from another activity? 如何在另一个活动中调用此方法? - How can I call this method in another activity? 如何在Android的另一个活动中使用一个活动中的变量? - How do I use a variable from one activity in another activity in Android? Android - 如何将ImageView从一个活动转移到另一个活动? - Android - how can i transfer ImageView from one activity to another activity? 如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio - How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio 我如何通过使用Intent从Android中的另一个常规活动中调用特定的选项卡活动 - How can i call a specific tab activity from another regular activity in android by using intent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM