NotificationID не может быть преобразован в переменную - PullRequest
0 голосов
/ 05 августа 2011

Я только начал изучать Android, у меня ограниченные знания java, но я полу-способен с c / c ++, целью C и т. Д. ... В настоящее время я работаю над книгой p2pwrox под названием «Начало разработки приложений для Android», которую я принес, однако я идуотклеить с помощью «Попробуйте это: Показать уведомления в строке состояния» на pg73.

Мне удалось написать все это сладко и я привыкаю к ​​Android SDK и Eclipse Ide, однако я получил этоОшибка в моем файле NotificationActivity.java, показанном ниже, что я просто не знаю, как исправить.

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

Любые идеи, которые могут меня услышать, будут высоко оценены, я пытался очистить свою сборку, но это не помоглочто-нибудь, чтобы помочь.

1 Ответ

3 голосов
/ 05 августа 2011

У вас нет переменной с именем 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);
    }
}
...