最简单的方法是在启动时将主类/对象引用传递给子类,即在构造函数中。并将引用存储在成员变量中以便稍后使用它们。
忘记尝试从活动之外访问成员。
让你的活动实施 GesturePerformedListener
GesturePerformedListener
public class MyActivity implements OnGesturePerformedListener { //your activity code here }
然后而不是创建一个 common 当state听者只是使用时的类 this :
common
this
gestures.addOnGesturePerformedListener(this);
这消除了对单独类的需要,并允许您直接访问TextView
在构造函数中传递对活动的引用,并添加相关方法以在类之间进行通信(不要直接访问Activity的字段...)
在活动中:
common myHandler = new common(this);
在Common中(使用大写C表示名称约定):
public class Common implements OnGesturePerformedListener { private YourActivityClass activity; public Common(YourActivityClass activity) { this.activity = activity; } // Rest of code }