【问题标题】:How to resolve error "non static method 'getDeviceId()' cannot be referenced by static context" [duplicate]如何解决错误“静态上下文无法引用非静态方法'getDeviceId()'” [重复]
【发布时间】:2015-08-30 20:11:18
【问题描述】:

我正在尝试在以下代码中获取设备 ID

    public String getDevId() {
        String devId=android.telephony.TelephonyManager.getDeviceId();
        return devId;
    }

但它是说“静态上下文不能引用非静态方法'getDeviceId()'”

【问题讨论】:

  • 首先清理你的Core Java。
  • 我试过TelephonyManager xx= new TelephonyManager();但它说“TelephonyManager()”不是公开的
  • 包括你的 logcat,这样我们就可以看到发生了什么

标签: java android


【解决方案1】:
public String getDeviceId(Context context){
    TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
      return telephonyManager.getDeviceId();
}

【讨论】:

  • 报错cannot resolve getSystemService in java.lang.string
  • 你需要一个上下文来访问 getSystemService
  • 你为什么直接跳转到 TelephonyManager。首先从Introduction to Android开始
  • 嗯,我是安卓新手。我只是在尝试。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多