在安卓中无法解析getContext()方法

7

我正在尝试实现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


4
将getContext()替换为"this"进行尝试。 - Akash kumar
3个回答

7

getContext()方法在View类中可用。使用MainActivity.this来访问getContentResolver方法:

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

4

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

You are using activity so You don't need to use getcontext() or this

Just use below line of code to get you the string:-

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

1
你可以使用 this
 String android_id= Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接