【问题标题】:Java - Pass static method data to method DynamicallyJava - 将静态方法数据动态传递给方法
【发布时间】:2018-07-25 16:03:26
【问题描述】:

所以我试图在我的应用程序中不断更新 Box 的位置,但我检索数据的方式是来自静态方法。 我试图做以下但没有运气.. 希望有人能指导我如何正确地做到这一点。

Public class Foo extends service{
private ImageView box;
    static int dataX;
    static int dataY;

public void onCreate() {

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    box = new ImageView(this);
    box.setImageResource(R.drawable.imageX);


final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

                params.x = 0
                params.y = 0
        windowManager.addView(box, params);

           // The Box position should be changing constantly from the data I retrieve 

               box.setX(dataX);
               box..setY(dataY);
            windowManager.updateViewLayout(box, params);



..
..

// and the code above is not working, since im not passing the data correctly


}

public static void Data(int x, int y) {
// the data is being passed to this method
// x and y contains dynamic data which changes constantly
 dataX = x;
 dataY = y;

}

}

【问题讨论】:

  • 为什么使用静态方法?是必须的吗?
  • 是的,在这种情况下它对我来说是必须的 :-)
  • 好的,我认为您不应该更改 onCreate() 方法中的位置,因为它只调用一次尝试查找方法会频繁调用或尝试删除并再次重新绘制以强制 onCreate () 工作
  • @Sundar 说:“它应该可以工作。由于您的代码很好,能否请您显示您尝试设置为 Box 的代码。由于它是静态变体,它包含的数据是什么我们在 Data 方法中设置”,我同意:如果 Box 与 Point 类似,那么我看不出您发布的代码有任何问题
  • 但它没有:-)。我提供了更多代码

标签: java android static-methods


【解决方案1】:
Public class Foo extends service{
private ImageView box;
    static int dataX;
    static int dataY;

public void onCreate() {

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    box = new ImageView(this);
    box.setImageResource(R.drawable.imageX);


    //Did you call the method like this?
      Data(dataX, dataY);
    //

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

                params.x = 0
                params.y = 0
        windowManager.addView(box, params);

           // The Box position should be changing constantly from the data I retrieve 

               box.setX(dataX);
               box..setY(dataY);
            windowManager.updateViewLayout(box, params);



..
..

// and the code above is not working, since im not passing the data correctly


}

public static void Data(int x, int y) {
// the data is being passed to this method
// x and y contains dynamic data which changes constantly
 dataX = x;
 dataY = y;

}

}

【讨论】:

  • 在这种情况下,我无法使其成为非静态的。为什么我不能只保存价值?我确实在问题中添加了更多代码,请看一下
  • 查看我的更新答案。这是你要找的吗?
  • 去掉static这个词,你从哪里得到数据?
  • 它很复杂.. 但是来自 JNI 函数
【解决方案2】:

重写:

如果这是针对 rmi 的,则使用静态方法将 rmi 接口分解为它自己的类。应该很简单。

所有工作都可以在另一个非静态(普通)类中完成,并且您的静态 rmi 类可以保留对该实例的引用。

【讨论】:

  • 不确定您的意思?我应该在哪里调用 onCreate 方法?我不能在静态方法中调用它。
  • 如果我把所有的逻辑都放在 Data() 方法中.. 那么它永远不会被创建?我必须在 onCreate 中添加它???
  • 而且windowManager是一个服务..你也不能在静态方法中使用服务对象。
  • 我完全理解你为什么说它应该在 Data() 方法中设置。但它不可能吗?我似乎无法让它工作....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多