Android 从另一个类调用方法 [英] Android Call a method from another class
本文介绍了Android 从另一个类调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这个问题重复了,但我在网上找不到答案.
I know that this Question is repeated but I can't find the answer in the Internet.
我想从另一个类调用一个方法.
I want to call a method from another class.
我有 Class1 和 Class2.
I have Class1 and Class2.
在第 2 类我有这个方法:
In Class 2 I have this method:
public void UpdateEmployee(){
//some code
}
我想从 Class1 调用上面的方法.
I want to call the above method from Class1.
感谢您的回答.
----编辑----
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)
推荐答案
你应该使用以下代码:
Class2 cls2 = new Class2();
cls2.UpdateEmployee();
如果您不想创建一个新实例来调用该方法,您可以将该方法声明为 static
,然后您只需调用 Class2.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()
.
这篇关于Android 从另一个类调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文