【问题标题】:how to get calling method name and lineNumber in Android如何在Android中获取调用方法名称和lineNumber
【发布时间】:2013-12-17 19:37:26
【问题描述】:

我需要在android中获取调用方法名称和行号,我试过了

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

但这会给出调用方法名称,但我需要打印调用方法名称,谁能帮我解决这个问题。

【问题讨论】:

    标签: android


    【解决方案1】:

    类名

    this.getClass().getName();
    

    行号

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

    方法是

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

    getStackTrace() 访问数组中的正确元素。

    【讨论】:

    • 嗨,如果我使用 getStackTrace()[1],而不是使用 getStackTrace()[1],它会以 getStackTrace 作为方法名返回,我需要显示调用方法名...
    【解决方案2】:

    试试这样的:

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

    【讨论】:

      猜你喜欢
      • 2013-07-07
      • 1970-01-01
      • 2011-10-27
      • 2011-07-03
      • 1970-01-01
      • 2013-06-09
      • 2014-07-11
      • 1970-01-01
      • 2012-03-27
      相关资源
      最近更新 更多