无法在Android中解析方法getContext()

时间:2015-12-08 04:40:38

标签: android

我正在尝试实施GCM推送通知并尝试获取Android模拟器ID,并添加以下代码,但它显示以下错误。我在这个平台上相对较新。

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

}

enter image description here

3 个答案:

答案 0 :(得分:5)

View类中提供了

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

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

答案 1 :(得分:4)

嘿,这是你问题的解决方案: -

  

您正在使用活动,因此不需要使用getcontext()this

     

只需使用下面代码行来获取字符串: -

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

答案 2 :(得分:1)

您可以使用this

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