使用Asynctask进行GCM注册时出现错误

3
我使用开发者网站上的示例代码,但在编译时出现错误。 http://developer.android.com/google/gcm/gs.html 请复制下面的代码。
private void registerBackground() {
new AsyncTask() {
    @Override
    protected String doInBackground(Void... params) {
        String msg = "";
        try {
            regid = gcm.register(GCM_SENDER_ID);
            msg = "Device registered, registration id=" + regid;

            // You should send the registration ID to your server over HTTP, 
            // so it can use GCM/HTTP or CCS to send messages to your app.

            // For this demo: we don't need to send it because the device  
            // will send upstream messages to a server that will echo back 
            // the message using the 'from' address in the message. 

            // Save the regid for future use - no need to register again.
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(PROPERTY_REG_ID, regid);
            editor.commit();
        } catch (IOException ex) {
            msg = "Error :" + ex.getMessage();
        }
        return msg;
    }
    // Once registration is done, display the registration status
    // string in the Activity's UI.
    @Override
    protected void onPostExecute(String msg) {
        mDisplay.append(msg + "\n");
    }
}.execute(null, null, null);
}

我在编译时遇到了错误,提示“Asynctask是原始类型。应该将引用的泛型类型参数化。”

1个回答

9

您尚未声明通用类型参数。

更改

new AsyncTask() {

为了

new AsyncTask<Void,Void,String>() {

同时,

execute(null, null, null);

可以更改为

execute();

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