我有一个主要的类,即“ A类”:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mView = new AppGLSurfaceView(this); <------- I am creating
}
// Log <--------------------------------------- our log function
public void LogInfo(String message) {
android.util.Log.i("MyLogTag", "Message:" + message);
}
}
类A包含此类的对象:
class AppGLSurfaceView extends GLSurfaceView {
public AppGLSurfaceView(Context context) {
super(context);
mContext = context;
}
public boolean onTouchEvent(final MotionEvent event) {
mContext.LogInfo ("onTouchEvent"); <--------------- FAIL
return false;
}
Context mContext;
}
从旁看,MyActivity似乎正在将自身的引用传递给第二类,mView = new AppGLSurfaceView(this)正在传递。“ this”是对象引用,对吗?
我通过“ mContext = context;”将该对象引用存储在类B中的变量中。
如何调用LogInfo方法?
(我很少使用Java,因此,如果它不是方法而是函数,请简要正确地说明我,我想知道Java术语。)
LogInfo(....)
是MyActivity
类的方法,您尝试调用一个Context
对象,则需要强制mContext
转换,例如:((MyActivity)mContext).LogInfo(....)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句