Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

java - The constructor Intent is undefined

I have the content module loaded, the specific error I'm getting is: The constructor Intent(new View.OnClickListener(){}, Class<ContactWidget>) is undefined

Any ideas on this? I got this from the tutorial here: 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);
        }
    };
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change this:

new Intent(this, ContactWidget.class);

to

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

The error happens because, in that case, this is referencing the instance of OnClickListener, but the Intent's constructor expects a Context. The context you have to pass is the reference to the activity itself, thus you have to access it explicitly using ContactWidget.this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...