java OnClickListener 无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4527616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OnClickListener cannot be resolved to a type
提问by Webnet
I'm diving into Java (this is day 1) and I'm trying to create a button that will trigger a notification when I click it...
我正在深入研究 Java(这是第 1 天)并且我正在尝试创建一个按钮,当我单击它时会触发通知......
This code is based off of the notification documentation here, and UI events documentation here
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);
}
}
}
I'm running into a problem: OnClickListener cannot be resolved to a type
. The problem here is that I don't see any problems with my code in relation to the example I'm using
我遇到了一个问题:OnClickListener cannot be resolved to a type
。这里的问题是我没有看到与我正在使用的示例相关的代码有任何问题
回答by Cristian
Add this import:
添加此导入:
import android.view.View.OnClickListener;
If you are using Eclipse, you can use Ctrl+Shift+Oto make it import those clases or interfaces automagically.
如果您使用的是Eclipse,你可以使用Ctrl+ Shift+ O,使之导入这些clases或接口自动的。
回答by user432209
Make sure you have both these imports:
确保你有这两个进口:
import android.view.View;
import android.view.View.OnClickListener;
回答by Kuba Paw?owski
setContentView(R.layout.main);
Should be above the button declaration, just below
应该在按钮声明之上,就在下面
super.onCreate(savedInstanceState);