2015-12-08 73 views
3

我试图实现GCM推送通知并尝试获取Android模拟器ID,并添加下面的代码,但它显示我以下错误。我在这个平台上看起来相对较新。无法解析Android中的方法getContext()

public class MainActivity extends AppCompatActivity { 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     String android_id= Settings.Secure.getString(getContext().getContentResolver(), Settings.Secure.ANDROID_ID); 

} 
在View类提供

enter image description here

+3

尝试用 “本” 上进行的getContext的() –

回答

4

getContext()方法。使用MainActivity.this访问getContentResolver方法:

String android_id= Settings.Secure.getString(MainActivity.this.getContentResolver(), 
              Settings.Secure.ANDROID_ID) 
3

嘿这里是你的问题的解决: -

您正在使用活动所以你不需要使用getcontext()this

只需使用以下代码行就可以得到字符串: -

String string =Settings.Secure.getString(getContentResolver(),Settings.Secure.ANDROID_ID); 
0

您可以使用this

String android_id= Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);