我刚刚开始学习Android,我的Java知识有限,但是半天能用c / c ++目标C等...我目前正在编写一本名为Beginning Android Application Development的p2pwrox电子书,但是我来了在第73页的“试一试:在状态栏上显示通知”中取消。
我已经设法把它写得很好,我已经习惯了android sdk和eclipse ide,但是我在下面显示的NotificationActivity.java文件中遇到了这个错误,我只是不知道如何解决。< / p>
package com.androidtestingfun.www;
import android.app.Activity;
import android.os.Bundle;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class NotificationsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.btn_displaynotif);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
displayNotification();
}
});
}
protected void displayNotification()
{
//---PendingIntent to launch activity if the user selects this notification---
Intent i = new Intent(this, NotificationView.class);
i.putExtra("notificationID", notificationID); //-----the second parameter here is getting an error
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(
R.drawable.icon,
"Reminder: Meeting starts in 5 minutes",
System.currentTimeMillis());
CharSequence from = "System Alarm";
CharSequence message = "Meeting with customer at 3pm...";
notif.setLatestEventInfo(this, from, message, pendingIntent);
//---100ms delay, vibrate for 250ms, pause for 100ms and then vibrate for 500ms---
notif.vibrate = new long[] {100, 250, 100, 500};
nm.notify(notificationID, notif);//-----the first parameter here is getting an error
}
}
任何可以听到我的想法都会非常感激,我已经尝试过清理我的版本,但是没有做任何事情来帮助。
答案 0 :(得分:3)
您没有名为notificationID
的变量。
将此变量添加到类中,请参阅代码段示例:
public class NotificationsActivity extends Activity {
private static final int notificationID = 1234;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
// ........
}
protected void displayNotification()
{
//---PendingIntent to launch activity if the user selects this notification---
Intent i = new Intent(this, NotificationView.class);
i.putExtra("notificationID", notificationID);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(
R.drawable.icon,
"Reminder: Meeting starts in 5 minutes",
System.currentTimeMillis());
CharSequence from = "System Alarm";
CharSequence message = "Meeting with customer at 3pm...";
notif.setLatestEventInfo(this, from, message, pendingIntent);
notif.vibrate = new long[] {100, 250, 100, 500};
nm.notify(notificationID, notif);
}
}