简体   繁体   English

如何从另一个活动调用一个活动的 function

[英]How to call function of one activity from another activity

Actually i want to call a function of one activity from another activity..i write a simple code for that but its not working..and show runtime error..please check the code...if there any mistake..实际上我想从另一个活动中调用一个活动的 function..我为此编写了一个简单的代码,但它不起作用..并显示运行时错误..请检查代码...如果有任何错误..

code for activity1:活动1的代码:

public class Activity1 extends Activity2  
{
        public void onCreate(Bundle savedInstanceState) 
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main2);

                call();
        }
        public void call()
        {
                showToast("Helloo");
        }
}

code for activity2:活动代码2:

public class Activity2 extends Activity 
{

        public void onCreate(Bundle savedInstanceState) 
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
        }
        public void showToast(String s)
        {
                EditText t=(EditText)findViewById(R.id.editText1);
                t.setText(s);
        }
}

Your problem is that you're calling findViewById on a view that doesn't exist.您的问题是您在不存在的视图上调用findViewById

Activity1 is extending Activity2 . Activity1正在扩展Activity2

You call the super.onCreate in Activity1 which calls the onCreate in Activity2 which calls setContentView() for R.layout.main .您调用Activity2中的super.onCreate调用Activity1中的onCreate调用R.layout.mainsetContentView()

I'm guessing your text R.id.editText1 is in the main layout.我猜你的文字R.id.editText1在主布局中。

When Activity1 returns from the call to super.onCreate it immediately resets the content layout to main2 .Activity1从对super.onCreate的调用返回时,它会立即将内容布局重置为main2

The edit text box that you are trying to edit no longer exists.您尝试编辑的编辑文本框不再存在。 findViewById can not find it because the layout is not active. findViewById找不到它,因为布局未激活。 Thus it crashes.因此它崩溃了。

To fix it try this:要修复它,试试这个:

public class Activity1 extends Activity2  
{
        private EditText et;
        public void onCreate(Bundle savedInstanceState) 
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main2);

                et = (EditText) findViewById(R.id.editText2);
                call();
        }
        public void call()
        {
                showToast("Helloo", et);
        }
}

Where R.id.editText2 is an edit text box in the layout main2 .其中R.id.editText2是布局main2中的编辑文本框。

In Activity2:在活动 2 中:

public class Activity2 extends Activity 
{

        public void onCreate(Bundle savedInstanceState) 
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
        }
        public void showToast(String s, EditText t)
        {
                t.setText(s);
        }
}

First, this is a bad design principle since only one Activity is active at a time.首先,这是一个糟糕的设计原则,因为一次只有一个活动处于活动状态。 You can make a method static and then you can cross call them but at that point it should be in some sort of common util class.您可以创建一个方法 static,然后您可以交叉调用它们,但此时它应该在某种常见的 util class 中。

The simplest way is to declare your showToast() method as public static , this way you can call it without having an instance of Activity2 .最简单的方法是将您的showToast()方法声明为public static ,这样您就可以在没有Activity2实例的情况下调用它。

if you put it in as static you should declare it on your main activity如果你把它作为 static 你应该在你的主要活动中声明它

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

相关问题 如何在不创建要调用的活动的新对象的情况下从另一个活动调用一个活动的功能? - How to call a function of one activity from another activity without creating new object of the activity being called? 在一个活动中从另一个活动中调用函数的正确方法是什么? - What is the rigth way to call a function in one activity from another activity? 如何从一个活动调用按钮事件到另一个活动进行刷新? - How to call button event from one activity to another activity for refreshing? 如何从另一个活动中定义的一个活动中调用方法 - How to call a method from one activity which is defined in another activity 我如何从android中的另一个活动调用活动中的函数? - How can i call a function in activity from another activity in android? 如何从其他活动或服务调用一个活动的 function? - How to call function of one Activity from other Activity or service? 如何从另一个活动调用Gridview活动 - How to call the Gridview Activity from another activity 如何从另一个活动中调用TextToSpeech活动? - How to call TextToSpeech activity from another activity? 如何在Activity中调用另一个Activity中的方法 - how to call a method in another Activity from Activity 如何从一个活动导航到另一个活动 - How to navigate from one activity to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM