Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调用方法作为主活动_Java_Android_Methods - Fatal编程技术网

Java 调用方法作为主活动

Java 调用方法作为主活动,java,android,methods,Java,Android,Methods,我有一个方法,由于某种原因,它只有在从主活动调用时才能很好地工作。问题是我试图从另一个类调用它。 是否有方法调用显示为主活动的方法?如果是静态方法,请像这样调用:MyActivityClass.myMethod(myArg1,myArg2)。确保您的方法是静态的,并且是公共的,而不是私有的。另外,也不要忘记包括类。之后,您应该能够使用该类对象的任何方法 通过在主循环器上创建处理程序,始终可以在主线程中运行代码: Handler handler= new Handler(Looper.getMai

我有一个方法,由于某种原因,它只有在从主活动调用时才能很好地工作。问题是我试图从另一个类调用它。
是否有方法调用显示为主活动的方法?

如果是静态方法,请像这样调用:MyActivityClass.myMethod(myArg1,myArg2)。

确保您的方法是静态的,并且是公共的,而不是私有的。另外,也不要忘记包括类。之后,您应该能够使用该类对象的任何方法

通过在主循环器上创建处理程序,始终可以在主线程中运行代码:

Handler handler= new Handler(Looper.getMainLooper());
那就打电话吧

handler.post(new Runnable(
  @Override
  public void run () {
    //  code that should be running from UI Thread
  }
));

当您使用来自不同类的方法时,需要创建定义该方法的类的实例。考虑下面两个类:

public class Test{ 

 public void showMesage(){ 
 System.out.println("I am method showMessage from class Test!"); 
 } 
 } 

 public class TestCall{ 

 public void run(){ 
 Test.showMesage(); 
 } 
 } 


 Class Test has a method named "showMessage". The method "run" in TestCall class is trying to use the showMessage method. This will generate similar error that you are having. To resolve that, you need to make an instance of the class Test inside the run method like this: 

 public void run(){ 
 Test t = new Test(); // create an instance 
 t.showMesage(); // use t to refer any method of class Test 
 } 

 Or, you can make the method showMessage static like this: 

 public static void showMesage(){ 
 System.out.println("I am method showMessage from class Test!"); 
 } 

 Now, you don't need to change the run method anymore. If you have any questions, post 'em. 

 Additional Suggestion: 

 You can always write a method which will extract the array list for you like this: 

 import java.util.ArrayList; 

 public class Test{ 

 ArrayList al = null; 

 public void populateList(){ 
 al = new ArrayList(); 
 al.add("Apple"); 
 al.add("Mango"); 
 } 

 public ArrayList getList(){ 
 return this.al; 
 } 
 } 

 import java.util.ArrayList; 

 public class TestCall{ 

 public void run(){ 
 Test t = new Test(); 
 t.populateList(); 
 ArrayList aList = t.getList(); 
 System.out.println(aList.get(0)); 
 System.out.println(aList.get(1)); 
 } 

 public static void main(String[] strARgs){ 

 new TestCall().run(); 
 } 

 } 

 Further response: 

 In that case, you can declare the ArrayList as static so that you can access that arraylist from other class directly. For example, class Mango as a arraylist named aList like: 

 class Mango { 

 public static ArrayList aList = null; //this is a global variable 

 //some methods and stuff 
 ....... 
 ..... 
 } 

 This aList now can be accessed easily from other classes like: 

 import Mango; 
 class Apple { 
 ArrayList aList = Mango.AList; // direct call 
 ... 
 .... 
 } 
 Another thing, before you access the arraylist, you need to make sure that the event that populates the arraylist(dynamically) has been called already from the same class where you are trying to access the arraylist. 

查看您的代码肯定会有帮助。发布您的logcat输出也会有帮助。你需要更具体地说明什么不起作用。这正是我想要的。我肯定能读懂你的心思=)。“主活动”和主线程(UI线程)——这是完全不同的东西。我刚才说的是androidPlease格式的主活动,在提供一个时,请给出代码和答案。