0

I need to get calling method name and line number in android,I tried with

Thread.currentThread().getStackTrace()[2].getMethodName())

but this will give called method name,but I need to print calling method name,can anyone help me in solving this.

2 Answers 2

1

For the class name

this.getClass().getName();

For line number

Thread.currentThread().getStackTrace()[1].getLineNumber();

and the method would be

Thread.currentThread().getStackTrace()[1].getMethodName();

To access the right element from the array from the getStackTrace().

Sign up to request clarification or add additional context in comments.

1 Comment

Hi,instead of using getStackTrace()[2],if i use getStackTrace()[1],it is returning as getStackTrace as method name,i need to display the calling method name...
0

Try something like this :

method2(){
method1("method2");
}
method3(){
method1("method3");
}
method1(String Callingname){
Log.d("Method1", "Callingname")
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.