构造函数Intent未定义


问题内容

我已经加载了内容模块,我得到的具体错误是: The constructor Intent(new View.OnClickListener(){}, Class<ContactWidget>) is undefined

有什么想法吗?我是从以下教程中获得的:http
:
//developer.android.com/guide/topics/ui/notifiers/notifications.html

package com.example.contactwidget;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ContactWidget extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button calc1 = (Button) findViewById(R.id.calc_button_1);
        calc1.setOnClickListener(buttonListener);

        setContentView(R.layout.main);
    }

    private static final int HELLO_ID = 1;

    private OnClickListener buttonListener = new OnClickListener() {
        public void onClick (View v) {
            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

            int icon = R.drawable.icon;
            CharSequence ticketBrief = "Button Pressed Brief";
            CharSequence ticketTitle = "Button pressed";
            CharSequence ticketText = "You pressed button 1";
            long when = System.currentTimeMillis();

            Notification notification = new Notification(icon, ticketBrief, when);

            Intent notificationIntent = new Intent(this, ContactWidget.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

            notification.setLatestEventInfo(getApplicationContext(), ticketTitle, ticketText, contentIntent);

            mNotificationManager.notify(HELLO_ID, notification);
        }
    };
}

问题答案:

更改此:

new Intent(this, ContactWidget.class);

new Intent(ContactWidget.this, ContactWidget.class);

发生错误是因为在这种情况下,this它引用的实例OnClickListener,但Intent's构造函数需要一个Context。您必须传递的上下文是对活动本身的引用,因此您必须使用显式访问它ContactWidget.this