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...
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
Add this import:
import android.view.View.OnClickListener;
If you are using Eclipse, you can use Ctrl+Shift+O to make it import those clases or interfaces automagically.
Make sure you have both these imports:
import android.view.View;
import android.view.View.OnClickListener;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With