如何从 Android Activity 调用 Java 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6093594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a Java class from a Android Activity
提问by M.A.Murali
I have an Android activity called main.java and BPMClass.java class. I need to call the BPMClass.java within the main.java class. I wrote the code in the following way (does not show function name):
我有一个名为 main.java 和 BPMClass.java 类的 Android 活动。我需要在 main.java 类中调用 BPMClass.java。我按照以下方式编写代码(不显示函数名称):
import com.app.BPMClass;
...
public class main extends Activity {
BPMClass bpmclass;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bpmclass = new BPMClass("filename");
bpmclass. // does not show the function name of BPMClass.
}
}
....
BPMClass{
String fname;
public BPMClass(String filename){
fname=filename
}
public int fun1(fname){
int val=0;
.......
return val;
}
}
采纳答案by Vladimir Ivanov
If BPMClass
is declared as public, then code is fine.
如果BPMClass
声明为 public,则代码没问题。
回答by Zoombie
Declared access modifier of class as according to your need, if class needed to access from its package level, then this should work fine.. else declare it as public
根据您的需要声明类的访问修饰符,如果类需要从其包级别访问,那么这应该可以正常工作..否则将其声明为公共
回答by PedroAGSantos
problem is simple if he try to place class after name of the class
问题很简单,如果他尝试在班级名称之后放置班级
class BPMClass{
String fname;
public BPMClass(String filename){
fname=filename
}
public int fun1(fname){
int val=0;
.......
return val;
}
}