构造意图是不确定的 [英] The constructor Intent is undefined
问题描述
我的内容模块加载,具体的错误我得到的是:构造意图(新View.OnClickListener(){},类< ContactWidget>)的未定义
对此有何想法?我得到这个从这里教程:<一href="http://developer.android.com/guide/topics/ui/notifiers/notifications.html">http://developer.android.com/guide/topics/ui/notifiers/notifications.html
包com.example.contactwidget;
进口android.app.Activity;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
公共类ContactWidget延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
按钮CALC1 =(按钮)findViewById(R.id.calc_button_1);
calc1.setOnClickListener(buttonListener);
的setContentView(R.layout.main);
}
私有静态最终诠释HELLO_ID = 1;
私人OnClickListener buttonListener =新OnClickListener(){
公共无效的onClick(视图v){
字符串NS = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =(NotificationManager)getSystemService(NS);
INT图标= R.drawable.icon;
CharSequence的ticketBrief =按钮pressed简报;
CharSequence的ticketTitle =按钮pressed;
CharSequence的ticketText =您pressed按钮1;
时长= System.currentTimeMillis的();
通知通知=新的通知(图标,ticketBrief时);
意图notificationIntent =新的意图(这一点,ContactWidget.class);
PendingIntent contentIntent = PendingIntent.getActivity(此,0,notificationIntent,0);
notification.setLatestEventInfo(getApplicationContext(),ticketTitle,ticketText,contentIntent);
mNotificationManager.notify(HELLO_ID,通知);
}
};
}
更改此:
新意图(这一点,ContactWidget.class);
到
新意图(ContactWidget.this,ContactWidget.class);
错误发生,因为,在这种情况下,这
所引用 OnClickListener
的实例,但意图的
构造函数使用上下文
。你必须通过上下文的参照活动本身,因此,你必须明确地使用访问它 ContactWidget.this
。
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);
}
};
}
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
.
这篇关于构造意图是不确定的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!