私のAndroidアプリは私にこのエラーを出し続けています。The application has stopped working unexpectedly. Please try again
。コードの何が問題なのかわかりません。私はこのアプリに何時間も取り組んでいますが、今は頭がバラバラになっています。コードで何が間違っているのですか?
xmlファイル
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:title="Enable/Disable Alarm"
android:defaultValue="true"
android:key="cbAlarm"
android:summary="Enable or disable alarm for full charged battery" />
<RingtonePreference
android:title="Choose Alarm"
android:key="ringtone"
android:summary="Choose ringtone as alarm for battery charged complete"
></RingtonePreference>
</PreferenceScreen>
メインクラスファイル
package com.zafar.batteryalarm;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
public class BatteryAlarm extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.alarm);
final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("cbAlarm");
checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (newValue.toString().equals("true")) {
startService(new Intent(getBaseContext(), BatteryService.class));
} else if(newValue.toString().equals("false")) {
stopService(new Intent(getBaseContext(), BatteryService.class));
}
return true;
}
});
}
}
これが私のサービスクラスです
package com.zafar.batteryalarm;
import java.io.IOException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.widget.Toast;
public class BatteryService extends Service {
// protected static final Bundle Bundle = null;
private MediaPlayer mMediaPlayer;
private static final int NOTIFICATION_ID = 1;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
initNotification();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
return START_STICKY;
}
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
private BroadcastReceiver batteryInfoReceiver;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Battery alarm is enabled", Toast.LENGTH_LONG).show();
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
if (plugged == 2) {
if (level == 100) {
Toast.makeText(context, "Battery charge is completed. Unplug charger!", Toast.LENGTH_LONG).show();
SharedPreferences getAlarms = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
String alarms = getAlarms.getString("ringtone",
"default ringtone");
// getting uri from MediaStore via filepath i.e.
// content://media/internal/audio/media/29
Uri uri = Uri.parse(alarms);
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(context, uri);
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (IOException e) {
// System.out.println("OOPS");
e.getStackTrace();
}
}
} else if (plugged == 0) {
mMediaPlayer.stop();
unregisterReceiver(this.batteryInfoReceiver);
// onDestroy();
}
}
};
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Battery alarm is disabled", Toast.LENGTH_LONG).show();
cancelNotification();
// unregisterReceiver(this.batteryInfoReceiver);
}
private void initNotification() {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Battery Service";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags = Notification.FLAG_ONGOING_EVENT;
Context context = getApplicationContext();
CharSequence contentTitle = "Content Title";
CharSequence contentText = "Content Text";
Intent notificationIntent = new Intent(this, BatteryAlarm.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
private void cancelNotification() {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.cancel(NOTIFICATION_ID);
}
}
編集
これが私のログキャットです
05-06 19:25:03.425: I/System.out(861): Sending WAIT chunk
05-06 19:25:03.425: W/ActivityThread(861): Application com.zafar.batteryalarm is waiting for the debugger on port 8100...
05-06 19:25:03.470: I/dalvikvm(861): Debugger is active
05-06 19:25:03.625: I/System.out(861): Debugger has connected
05-06 19:25:03.625: I/System.out(861): waiting for debugger to settle...
05-06 19:25:03.825: I/System.out(861): waiting for debugger to settle...
05-06 19:25:04.025: I/System.out(861): waiting for debugger to settle...
05-06 19:25:04.225: I/System.out(861): waiting for debugger to settle...
05-06 19:25:04.425: I/System.out(861): waiting for debugger to settle...
05-06 19:25:04.625: I/System.out(861): waiting for debugger to settle...
05-06 19:25:04.825: I/System.out(861): waiting for debugger to settle...
05-06 19:25:05.030: I/System.out(861): debugger has settled (1358)
05-06 19:25:05.250: D/dalvikvm(861): GC_EXTERNAL_ALLOC freed 46K, 50% free 2726K/5379K, external 0K/0K, paused 74ms
05-06 19:25:05.400: D/dalvikvm(861): GC_EXTERNAL_ALLOC freed 48K, 49% free 2775K/5379K, external 504K/518K, paused 16ms
05-06 19:25:05.510: D/CLIPBOARD(861): Hide Clipboard dialog at Starting input: finished by someone else... !
05-06 19:25:16.280: W/KeyCharacterMap(861): No keyboard for id 0
05-06 19:25:16.280: W/KeyCharacterMap(861): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-06 19:25:16.535: D/CLIPBOARD(861): Hide Clipboard dialog at Starting input: finished by someone else... !
05-06 19:30:12.350: E/MediaPlayer-JNI(861): setDataSource: outside path in JNI is ?x@
05-06 19:30:12.370: W/MediaPlayer(861): info/warning (1, 26)
05-06 19:30:12.370: E/MediaPlayer(861): error (-4, -4)
05-06 19:31:07.360: D/AndroidRuntime(861): Shutting down VM
05-06 19:31:07.360: W/dalvikvm(861): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-06 19:31:07.420: E/AndroidRuntime(861): FATAL EXCEPTION: main
05-06 19:31:07.420: E/AndroidRuntime(861): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in com.zafar.batteryalarm.BatteryService$1@40542be8
05-06 19:31:07.420: E/AndroidRuntime(861): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
05-06 19:31:07.420: E/AndroidRuntime(861): at android.os.Handler.handleCallback(Handler.java:587)
05-06 19:31:07.420: E/AndroidRuntime(861): at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 19:31:07.420: E/AndroidRuntime(861): at android.os.Looper.loop(Looper.java:130)
05-06 19:31:07.420: E/AndroidRuntime(861): at android.app.ActivityThread.main(ActivityThread.java:3691)
05-06 19:31:07.420: E/AndroidRuntime(861): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 19:31:07.420: E/AndroidRuntime(861): at java.lang.reflect.Method.invoke(Method.java:507)
05-06 19:31:07.420: E/AndroidRuntime(861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-06 19:31:07.420: E/AndroidRuntime(861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-06 19:31:07.420: E/AndroidRuntime(861): at dalvik.system.NativeStart.main(Native Method)
05-06 19:31:07.420: E/AndroidRuntime(861): Caused by: java.lang.NullPointerException
05-06 19:31:07.420: E/AndroidRuntime(861): at com.zafar.batteryalarm.BatteryService$1.onReceive(BatteryService.java:74)
05-06 19:31:07.420: E/AndroidRuntime(861): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
05-06 19:31:07.420: E/AndroidRuntime(861): ... 9 more
編集
Logcat
これは後の最新のlogcatですif(mMediaPlayer != null) { mMediaPlay.stop() }
が、それでも機能しません。
05-07 18:35:10.000: W/ActivityThread(28850): Application com.zafar.batteryalarm is waiting for the debugger on port 8100...
05-07 18:35:10.010: I/System.out(28850): Sending WAIT chunk
05-07 18:35:10.090: I/dalvikvm(28850): Debugger is active
05-07 18:35:10.210: I/System.out(28850): Debugger has connected
05-07 18:35:10.210: I/System.out(28850): waiting for debugger to settle...
05-07 18:35:10.410: I/System.out(28850): waiting for debugger to settle...
05-07 18:35:10.605: I/System.out(28850): waiting for debugger to settle...
05-07 18:35:10.805: I/System.out(28850): waiting for debugger to settle...
05-07 18:35:11.005: I/System.out(28850): waiting for debugger to settle...
05-07 18:35:11.210: I/System.out(28850): waiting for debugger to settle...
05-07 18:35:11.415: I/System.out(28850): waiting for debugger to settle...
05-07 18:35:11.614: I/System.out(28850): waiting for debugger to settle...
05-07 18:35:11.814: I/System.out(28850): debugger has settled (1489)
05-07 18:35:11.970: D/dalvikvm(28850): GC_EXTERNAL_ALLOC freed 51K, 50% free 2726K/5379K, external 0K/0K, paused 31ms
05-07 18:35:12.105: D/dalvikvm(28850): GC_EXTERNAL_ALLOC freed 21K, 49% free 2780K/5379K, external 504K/518K, paused 16ms
05-07 18:35:12.215: D/CLIPBOARD(28850): Hide Clipboard dialog at Starting input: finished by someone else... !
05-07 18:36:11.614: E/MediaPlayer-JNI(28850): setDataSource: outside path in JNI is ?x@