需要帮助设置警报管理器的通知

时间:2015-03-10 16:21:26

标签: java android alarmmanager android-notifications

我是初学程序员,这是我的第一个应用程序。我试图实施一个提醒,用户可以在一段时间内设置闹钟,并选择提醒他们。我试图将页面的ID传递给AlartReceiver类并返回到类(BillPageFragment),以便在用户单击警报通知时可以打开确切的页面但我不断收到NullPointerException。我没有正确传递ID(UUID)吗?请帮忙。 这是Id的密钥和setAlarm方法(BillPageFragment类)

public static final String EXTRA_BILL_ID = "com.styles.android.familybudgetapp.bill_id";

public void setAlarm(View view){
    Log.d(TAG, "alarm going!!...");

    Calendar c = Calendar.getInstance();
    c.setTime(mBill.getDate());
    Long alertTime = c.getTimeInMillis();
    UUID sendBill = mBill.getId();

    Intent alertIntent = new Intent(getActivity(), AlertReceiver.class);
    alertIntent.putExtra(AlertReceiver.EXTRA_BILL_ALARM_ID, sendBill);

     AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime,
    PendingIntent.getBroadcast(getActivity(), 1, alertIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));


}

这是AlertReceiver类

public class AlertReceiver扩展BroadcastReceiver {

private static final String TAG = "AlertReceiver";
public static final String EXTRA_BILL_ALARM_ID =
        "com.styles.android.familybudgetapp.billalarmid";

Bill mBill;
UUID BillId;
@Override
public void onReceive(Context context, Intent arg1) {

    BillId = (UUID)arg1.getSerializableExtra(EXTRA_BILL_ALARM_ID);
    String quu = BillId.toString();
    Log.d(TAG, quu);


    createNotication(context, "Bill Reminder", " Bill Due", "Alert");

}

public void createNotication(Context context, String msg,
        String msgText, String msgAlert) {
    // TODO Auto-generated method stub


    Intent i = new Intent(context, BillPageActivity.class);
    i.putExtra(BillPageFragment.EXTRA_BILL_ID, BillId);

    PendingIntent notificIntent = PendingIntent.getActivity(context, 0,
            i, 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                   .setSmallIcon(R.drawable.ic_perm_group_device_alarms)
                   .setContentTitle(msg)
                   .setTicker(msgAlert)
                   .setContentText(msgText);

    mBuilder.setContentIntent(notificIntent);

    mBuilder.setDefaults(Notification.DEFAULT_ALL);

    mBuilder.setAutoCancel(true);

    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(1, mBuilder.build());
        }

}

以下是从AlertReceiver类接收的BillPageActivity类

public class BillPageActivity extends SingleFragmentActivity {
private static final String TAG = "BillPageActivity";


@Override
protected Fragment createFragment() {
    // TODO Auto-generated method stub
    UUID billId = (UUID)getIntent()
            .getSerializableExtra(BillPageFragment.EXTRA_BILL_ID);

    return BillPageFragment.newInstance(billId);
}

}

这是整个BillPageFragment类

public class BillPageFragment extends Fragment {

private static final String TAG = "BillPageFragment";

public static final String EXTRA_BILL_ID =
        "com.styles.android.familybudgetapp.bill_id";

private static final String DIALOG_CHOICE = "choicebillpicker";
private static final String DIALOG_BILL_TIMEPICKER = "timebillpicker";
private static final String DIALOG_BILL_DATEPICKER = "datebillPicker";

private static final int REQUEST_BILL = 2;
private static final int REQUEST_BILL_DATE = 3;
private static final int REQUEST_BILL_TIME = 4;

Bill mBill;
Button mBillChoose;
Button mBillDate;
Button mBillTime;
Button mBillSet;
Date setTime;
android.text.format.DateFormat df = new android.text.format.DateFormat();

public static BillPageFragment newInstance(UUID billId){
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_BILL_ID, billId);

    BillPageFragment fragment = new BillPageFragment();
    fragment.setArguments(args);
    return fragment;

}



public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    UUID billId = (UUID)getArguments().getSerializable(EXTRA_BILL_ID);
    mBill = BillLab.get(getActivity()).getBill(billId);
    setHasOptionsMenu(true);



}

public void updateDate() {
    setTime = mBill.getDate();


    mBillDate.setText(DateFormat.format("dd MMM yyyy", setTime));
}

public void updateTime(){
    setTime = mBill.getDate();

    mBillTime.setText(DateFormat.format("h:mm a", setTime));
}

public void updateChoice(){
    mBillChoose.setText(mBill.getChoice());

}

public void manageBillDateDialog(){
    FragmentManager fm4 = getActivity()
            .getSupportFragmentManager();
    DurationDialogFragment dialogDate = DurationDialogFragment.newInstance(mBill.getDate());
    dialogDate.setTargetFragment(BillPageFragment.this, REQUEST_BILL_DATE);
    dialogDate.show(fm4, DIALOG_BILL_DATEPICKER);
}

public void manageBillTimeDialog(){
    FragmentManager fm4 = getActivity()
            .getSupportFragmentManager();
    TimeDialogFragment dialogTime = TimeDialogFragment.newInstance(mBill.getDate());
    dialogTime.setTargetFragment(BillPageFragment.this, REQUEST_BILL_TIME);
    dialogTime.show(fm4, DIALOG_BILL_TIMEPICKER);
}

public void setAlarm(View view){
    Log.d(TAG, "alarm going!!...");

    Calendar c = Calendar.getInstance();
    c.setTime(mBill.getDate());
    Long alertTime = c.getTimeInMillis();
    UUID sendBill = mBill.getId();

    Intent alertIntent = new Intent(getActivity(), AlertReceiver.class);
    alertIntent.putExtra(AlertReceiver.EXTRA_BILL_ALARM_ID, sendBill);

     AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime,
    PendingIntent.getBroadcast(getActivity(), 1, alertIntent,
            PendingIntent.FLAG_UPDATE_CURRENT));


}


@TargetApi(11)
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_bill_reminder_edit, parent, false);

    mBillChoose = (Button)v.findViewById(R.id.bill_title);
    updateChoice();
    mBillChoose.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            FragmentManager fm3 = getActivity()
                    .getSupportFragmentManager();
            BillSelectDialogFragment dialogThree = new BillSelectDialogFragment();
            dialogThree.setTargetFragment(BillPageFragment.this, REQUEST_BILL);
            dialogThree.show(fm3, DIALOG_CHOICE);

        }

    });

    mBillDate = (Button)v.findViewById(R.id.bill_date);
    updateDate();
    mBillDate.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            manageBillDateDialog();


        }

    });

    mBillTime= (Button)v.findViewById(R.id.bill_time);
    updateTime();
    mBillTime.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            manageBillTimeDialog();

        }

    });


    mBillSet = (Button)v.findViewById(R.id.bill_set);
    mBillSet.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            setAlarm(arg0);
            getActivity().onBackPressed();

        }

    });

    return v;

}

public void onActivityResult(int requestCode, int resultCode, Intent data){
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == 2){
        String billType = (String)data
                .getSerializableExtra(BillSelectDialogFragment.BILL_CHOICE);
        Log.d(TAG, billType);
        mBill.setChoice(billType);
        updateChoice();

    }
    if (requestCode == 3){
        Date date = (Date)data
                .getSerializableExtra(DurationDialogFragment.EXTRA_DATE);
        mBill.setDate(date);
        updateDate();
    }
    if (requestCode == 4){
        Date date = (Date)data
                .getSerializableExtra(TimeDialogFragment.EXTRA_DATE);
        mBill.setDate(date);
        updateTime();

    }
}

}

这是logcat中的错误

03-11 20:02:10.951: E/AndroidRuntime(12675): FATAL EXCEPTION: main
03-11 20:02:10.951: E/AndroidRuntime(12675): Process: com.styles.familybudgetapp, PID: 12675
03-11 20:02:10.951: E/AndroidRuntime(12675): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.styles.familybudgetapp/com.styles.familybudgetapp.BillPageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.styles.familybudgetapp.Bill.getChoice()' on a null object reference
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.os.Looper.loop(Looper.java:135)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.app.ActivityThread.main(ActivityThread.java:5221)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at java.lang.reflect.Method.invoke(Native Method)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at java.lang.reflect.Method.invoke(Method.java:372)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-11 20:02:10.951: E/AndroidRuntime(12675): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.styles.familybudgetapp.Bill.getChoice()' on a null object reference
03-11 20:02:10.951: E/AndroidRuntime(12675):    at com.styles.familybudgetapp.BillPageFragment.updateChoice(BillPageFragment.java:86)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at com.styles.familybudgetapp.BillPageFragment.onCreateView(BillPageFragment.java:131)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.app.Activity.performStart(Activity.java:5949)
03-11 20:02:10.951: E/AndroidRuntime(12675):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
03-11 20:02:10.951: E/AndroidRuntime(12675):    ... 10 more

三江源。

0 个答案:

没有答案