B. Nadolson
B. Nadolson

Reputation: 3058

Call Java method in current instance of containing object's class

I have a main class as such, "Class 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);
    }
}

Class A contains an object of this class:

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;
}

Off hand, it looks MyActivity is passing a reference of itself to the 2nd class, mView = new AppGLSurfaceView(this) is passing. "this" is the object reference, correct?

I am storing this object reference in class B in a variable via "mContext = context;"

How can I call the LogInfo method?

(I rarely use Java, so if it isn't method but rather a function, please briefly correctly me, I do want to know the Java terminology.)

Upvotes: 0

Views: 1035

Answers (4)

Ren&#233; Link
Ren&#233; Link

Reputation: 51393

You can change the constructor of AppGLSurfaceView to

class AppGLSurfaceView extends GLSurfaceView {

    MyActivity myActivity;

    public AppGLSurfaceView(MyActivity myActivity) {
        super(myActivity);
        this.myActivity = myActivity
    }

    public boolean onTouchEvent(final MotionEvent event) {
       myActivity.LogInfo ("onTouchEvent");
       return false;
    }
}

This introduces a dependency to MyActivity, so you can't create a AppGLSurfaceView anymore with any Context. Maybe you want to introduce an interface for logging.

Upvotes: 0

Titus
Titus

Reputation: 22474

LogInfo(....) is a method of the MyActivity class, you are trying to call in on a Context object, you need to cast the mContext in order to do that, ex: ((MyActivity)mContext).LogInfo(....)

Upvotes: 3

akhil_mittal
akhil_mittal

Reputation: 24157

As it seems you are passing an instance of class MyActivity to constructor of class AppGLSurfaceView, you can call method on instance of MyActivity as: ((MyActivity)mContext) .LogInfo()

On a side note you should use camel case for methods in Java (logInfo and not LogInfo). Also you don't need to declare method static as you want to call method in current instance of containing object's class.

Upvotes: 1

Sanjeev
Sanjeev

Reputation: 4375

Context is not an object of MainActivity, To use that function create its object or make that function as static in your MainActivity

public static void LogInfo(String message) {
    android.util.Log.i("MyLogTag", "Message:" + message);
}

and then in your A class use like this

MainActivity.LogInfo ("onTouchEvent"); 

Upvotes: 0

Related Questions