OnClickListener无法解析为类型

Web*_*net 5 java android

我正在深入研究Java(这是第1天),我正在尝试创建一个按钮,当我点击它时会触发通知...

这个代码是基于关闭通知文件的位置,并且UI事件文档在这里

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

    //Error: OnClickListener cannot be resolved to a type
    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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我遇到了一个问题:OnClickListener cannot be resolved to a type.这里的问题是我没有看到我的代码与我正在使用的示例有任何问题

Cri*_*ian 22

添加此导入:

import android.view.View.OnClickListener;
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Eclipse,则可以使用Ctrl+ Shift+ O使其自动导入这些clases或接口.