我正在制作一个简单的提醒应用。当提醒消失时,我收到空指针异常。
这里是Logcat的关键信息:引起:java.lang.NullPointerException at com.joshbgold.move.AlarmReceiver.onReceive(AlarmReceiver.java:34)
显然我没有正确初始化AlarmActivity。我知道这可能是一个简单的修复,感谢您对新的Java程序员的耐心。
AlarmReceiver.java:
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v4.content.WakefulBroadcastReceiver;
/**
* Created by JoshG on 7/6/2015.
*/
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
//MediaPlayer is used to play an mp3 file
final MediaPlayer mediaPlayer = MediaPlayer.create(context, R.drawable.om_mani_short);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaplayer) {
mediaplayer.stop();
mediaplayer.release();
}
});
mediaPlayer.start();
//this will update the UI with message
AlarmActivity inst = AlarmActivity.instance();
inst.setAlarmText("stretch");
//this will send a notification message
ComponentName comp = new ComponentName(context.getPackageName(),
AlarmService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
AlarmActivity.java:
package com.joshbgold.move;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;
import java.util.Calendar;
/**
* Created by JoshG on 7/6/2015.
*/
public class AlarmActivity extends Activity {
AlarmManager alarmManager;
private PendingIntent pendingIntent;
private TimePicker alarmTimePicker;
private static AlarmActivity inst;
private TextView alarmTextView;
public static AlarmActivity instance() {
return inst;
}
@Override
public void onStart() {
super.onStart();
inst = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
alarmTextView = (TextView) findViewById(R.id.alarmText);
ToggleButton alarmToggle = (ToggleButton) findViewById(R.id.alarmToggle);
final Button exitButton = (Button) findViewById(R.id.exitButton);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
final MediaPlayer mediaPlayer = MediaPlayer.create(this, R.drawable.om_mani_short);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaplayer) {
mediaplayer.stop();
mediaplayer.release();
}
});
mediaPlayer.start();
View.OnClickListener quitApp = new View.OnClickListener() { //this block stops music when exiting
@Override
public void onClick(View view) {
if (mediaPlayer != null) try {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
} catch (Exception e) {
Log.d("Alarm Activity", e.toString());
}
finish();
}
};
exitButton.setOnClickListener(quitApp);
}
public void onToggleClicked(View view) {
if (((ToggleButton) view).isChecked()) {
Log.d("MyActivity", "Alarm On");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
} else {
alarmManager.cancel(pendingIntent);
setAlarmText("Alarm Off");
Log.d("MyActivity", "Alarm Off");
}
}
public void setAlarmText(String alarmText) {
alarmTextView.setText(alarmText);
}
}
警报Service.java
package com.joshbgold.move;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
/**
* Created by JoshG on 7/6/2015.
*/
public class AlarmService extends IntentService {
private NotificationManager alarmNotificationManager;
public AlarmService() {
super("AlarmService");
}
@Override
public void onHandleIntent(Intent intent) {
sendNotification("stretch");
}
private void sendNotification(String msg) {
Log.d("AlarmService", "Preparing to send notification...: " + msg);
alarmNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, AlarmActivity.class), 0);
NotificationCompat.Builder alarmNotificationBuilder = new NotificationCompat.Builder(
this).setContentTitle("Reminder").setSmallIcon(R.mipmap.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
alarmNotificationBuilder.setContentIntent(contentIntent);
alarmNotificationManager.notify(1, alarmNotificationBuilder.build());
Log.d("AlarmService", "Notification sent.");
}
}
这是Logcat的内容。我把它设置为Log Level of Error:
07-07 18:53:13.119 13065-13065/com.joshbgold.move E/﹕ mali: REVISION=Linux-r3p2-01rel2 BUILD_DATE=Mon Nov 18 21:41:36 KST 2013
07-07 19:00:11.569 16746-16746/com.joshbgold.move E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start receiver com.joshbgold.move.AlarmReceiver: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2469)
at android.app.ActivityThread.access$1600(ActivityThread.java:158)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1372)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5365)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.joshbgold.move.AlarmReceiver.onReceive(AlarmReceiver.java:34)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2462)
at android.app.ActivityThread.access$1600(ActivityThread.java:158)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1372)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5365)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
//this will update the UI with message
AlarmActivity inst = AlarmActivity.instance();
inst.setAlarmText("stretch");
inst
是null
。您应该检查AlarmActivity.instance()
public static AlarmActivity instance() {
if (inst == null) {
// Do something such as inst = ...
}
return inst;
}
答案 1 :(得分:1)
如果这是班级,第34行是
inst.setAlarmText("stretch");
指向上面的行没有获得实际活动
AlarmActivity inst = AlarmActivity.instance();
检查AlarmActivity类,确保在使用instance()方法创建inst时,inst不为null。
您可以发布AlarmActivity类以了解如何创建/获取实例吗?
答案 2 :(得分:0)
我右转了一圈,并在提醒/闹钟响起时执行了不同的实施。虽然我正在做一些与最初预期不同的事情,但上面给出的答案让我朝着正确的方向前进。
以下是我用于AlarmReceiver.java的内容:
package com.joshbgold.move;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
//this will change to new activity for Reminder(s) at the appropriate time
Intent myIntent = new Intent();
myIntent.setClassName("com.joshbgold.move", "com.joshbgold.move.ReminderActivity");
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}