开发者

notificationID cannot be resolved to a variable

开发者 https://www.devze.com 2023-03-25 04:44 出处:网络
I have just started studying Android, I have limited java knowledge but am semi capable with c/c++ objective C etc... 开发者_如何学JAVAI am currently working through a p2pwrox ebook called Beginning A

I have just started studying Android, I have limited java knowledge but am semi capable with c/c++ objective C etc... 开发者_如何学JAVAI am currently working through a p2pwrox ebook called Beginning Android Application Development that I brought, however I am coming unstuck with the "Try it out: Display Notifications on the status bar" on pg73.

I have managed to write it all out sweet and am getting used to the android sdk and eclipse ide, however I have got this error in my NotificationActivity.java file shown below that I just don't know how to fix.

    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
    }
}

Any ideas that could hear me would be greatly appreciated, I have tried cleaning my build but that didnt do anything to help.


You do not have a variable called notificationID.

Add this variable to the class, see snippet example:

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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号