[英]Android Call an method from another class
I know that this Question is repeated but I can't find the answer in Internet. 我知道这个问题是重复的,但我在互联网上找不到答案。
I want to call a method from another class. 我想从另一个类调用一个方法。
I've Class1 and Class2. 我有Class1和Class2。
In Class 2 I've that method: 在第2课我有那种方法:
public void UpdateEmployee(){
//some code
}
I want to call that method from Class1. 我想从Class1调用该方法。
Thanks for any answer. 谢谢你的回答。
----EDIT---- - - 编辑 - -
final Button btnUpdate = (Button)findViewById(R.id.btnUpd);
btnUpdate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Employee updEmple = new Employee();
updEmple.UpdateEmployee();
}
});
----LogCat--- ---- logcat的---
05-28 16:30:44.030: E/AndroidRuntime(25198): FATAL EXCEPTION: main
05-28 16:30:44.030: E/AndroidRuntime(25198): java.lang.NullPointerException: println needs a message
05-28 16:30:44.030: E/AndroidRuntime(25198): at android.util.Log.println_native(Native Method)
05-28 16:30:44.030: E/AndroidRuntime(25198): at android.util.Log.v(Log.java:116)
05-28 16:30:44.030: E/AndroidRuntime(25198): at and.net.Employee.UpdateEmployee(Employee.java:77)
05-28 16:30:44.030: E/AndroidRuntime(25198): at and.net.AndActivity$2.onClick(AndActivity.java:51)
05-28 16:30:44.030: E/AndroidRuntime(25198): at android.view.View.performClick(View.java:2485)
05-28 16:30:44.030: E/AndroidRuntime(25198): at android.view.View$PerformClick.run(View.java:9080)
05-28 16:30:44.030: E/AndroidRuntime(25198): at android.os.Handler.handleCallback(Handler.java:587)
05-28 16:30:44.030: E/AndroidRuntime(25198): at android.os.Handler.dispatchMessage(Handler.java:92)
05-28 16:30:44.030: E/AndroidRuntime(25198): at android.os.Looper.loop(Looper.java:123)
05-28 16:30:44.030: E/AndroidRuntime(25198): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-28 16:30:44.030: E/AndroidRuntime(25198): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 16:30:44.030: E/AndroidRuntime(25198): at java.lang.reflect.Method.invoke(Method.java:507)
05-28 16:30:44.030: E/AndroidRuntime(25198): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-28 16:30:44.030: E/AndroidRuntime(25198): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-28 16:30:44.030: E/AndroidRuntime(25198): at dalvik.system.NativeStart.main(Native Method)
You should use the following code : 您应该使用以下代码:
Class2 cls2 = new Class2();
cls2.UpdateEmployee();
In case you don't want to create a new instance to call the method, you can decalre the method as static
and then you can just call Class2.UpdateEmployee()
. 如果您不想创建一个新实例来调用该方法,您可以将该方法decalre为
static
,然后您可以调用Class2.UpdateEmployee()
。
In Class1: 在Class1中:
Class2 inst = new Class2();
inst.UpdateEmployee();
And, if you don't want to instantiate Class2, declare UpdateEmployee as static and call it like this: 并且,如果您不想实例化Class2,请将UpdateEmployee声明为static并将其调用如下:
Class2.UpdateEmployee();
However, you'll normally want to do what @parag said. 但是,你通常想要做@parag所说的。
Add this in MainActivity. 在MainActivity中添加它。
Intent intent = new Intent(getApplicationContext(), Heightimage.class);
startActivity(intent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.