简体   繁体   English

构造函数Intent未定义

[英]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 我加载了内容模块,我得到的具体错误是: 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 我从这里的教程中得到了这个: 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 . 发生错误的原因是,在这种情况下, this是引用OnClickListener的实例,但Intent's构造函数需要Context The context you have to pass is the reference to the activity itself, thus you have to access it explicitly using ContactWidget.this . 您必须传递的上下文是对活动本身的引用,因此您必须使用ContactWidget.this显式访问它。

The context passed in could be from a remote application host... apparently you need the app context from your app, the one with your class (your broadcast reciever for instance). 传入的上下文可能来自远程应用程序主机......显然,您需要来自应用程序的应用程序上下文,与您的类(例如您的广播接收器)的应用程序上下文。 Intent intent = new Intent(context.getApplicationContext(), WidgetryBroadcastReceiver.class); Intent intent = new Intent(context.getApplicationContext(),WidgetryBroadcastReceiver.class); instead of Intent intent = new Intent(context, WidgetryBroadcastReceiver.class); 而不是Intent intent = new Intent(context,WidgetryBroadcastReceiver.class); go figure. 去搞清楚。

是的,为我工作以及错误

Geocoder gc = new Geocoder(this, Locale.getDefault());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM