在JAVA for Android中从其他类(非活动)调用类中的例程

在JAVA for Android中从其他类(非活动)调用类中的例程,android,class,android-context,Android,Class,Android Context,我有; -主游戏面板(扩展SurfaceView) -按钮(类) -水果经理(班级) -苹果(班级) 在MainGamePanel中,我检查是否有MotionEvent.ACTION\u DOWN我将其交给按钮。按钮检查坐标并将触摸布尔值设置为true 在MainGamePanel中,我检查是否有MotionEvent.ACTION\u UP如果button Touched=true,我将其交给按钮。按钮检查坐标和。。在FutureManager中执行例程。我错了: 这就是FrootManage

我有; -主游戏面板(扩展SurfaceView) -按钮(类) -水果经理(班级) -苹果(班级)

在MainGamePanel中,我检查是否有
MotionEvent.ACTION\u DOWN
我将其交给按钮。按钮检查坐标并将触摸布尔值设置为true

在MainGamePanel中,我检查是否有
MotionEvent.ACTION\u UP
如果button Touched=true,我将其交给按钮。按钮检查坐标和。。在FutureManager中执行例程。我错了:

这就是FrootManager的AddMoreFroot例程的样子:

public static void addFruit(MainGamePanel context){

fruitInventory();
//add 10 apples with random coordinates

for (int i = 0; i < 10; i++) {
Random Rnd = new Random();
apple nextApple = new apple(BitmapFactory.decodeResource(context.getResources(),
                  fruitResources.get(Rnd.nextInt(fruitResources.size()))));
//add them to the arraylist
MainGamePanel.AppleList.add(nextApple);
}
publicstaticvoidaddfruit(主游戏面板上下文){
水果库存();
//用随机坐标加10个苹果
对于(int i=0;i<10;i++){
随机Rnd=新随机();
apple Nextaple=new apple(BitmapFactory.decodeResource)(context.getResources(),
get(Rnd.nextInt(fruitResources.size());
//将它们添加到arraylist
MainGamePanel.AppleList.add(下一步);
}
}

如果我使用
fruitmanager.addfruit(这个)从MainGamePanel调用它,它工作得很好
我怀疑这与上下文有关。我不知道怎么修理。我已经阅读了有关上下文的内容,并理解了调用该类的“应用程序情况”。不过,我没有找到关于如何处理这种事情的n00b证明说明(抱歉;)。或者它可能与“静态”和“非静态”有关?在这里的黑暗中


非常感谢您的帮助。

因此您正在尝试使用水果管理器。addfruit(此);从按钮班? 如果是这种情况,它将不起作用,因为您将按钮对象传递给需要上下文的函数。在Button类中,您应该保留对上下文的引用,并在尝试添加水果时使用它

public class Button{
private Context context;
public Button(Context context)
{
this.context = context;
}
....
fruitmanager.addfruit(context);
...
从MainGamePanel创建一个如下按钮

new Button(this);
科尔古,多亏了你的回答,我才能够写作。非常感谢。