[英]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.main
的setContentView()
。
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.