gpt4 book ai didi

java - 应用程序在 android 中意外停止

转载 作者:太空狗 更新时间:2023-10-29 14:27:38 25 4
gpt4 key购买 nike

我的 Android 应用程序一直给我这个错误。 应用程序意外停止工作。请重试。我不知道我的代码有什么问题。我在这个应用程序上工作了几个小时,现在我的头都快炸开了。我的代码哪里做错了?

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

编辑

debugging image

逻辑猫这是 if(mMediaPlayer != null) { mMediaPlay.stop() } 之后的最新 logcat,但它仍然不起作用。

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@

最佳答案

你应该看看 logCat 的这一行:

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)

您在 BatteryService.java 的 onReceive 方法中的第 74 行有一个 NullPointer

确保所有变量都已初始化或正确处理。

编辑: 仔细查看代码后,问题可能出在

mMediaPlayer.stop();

可能会发生 mMediaPlayer 未初始化而返回 NullPointerException 的情况。

编辑 2:这应该修复 NullPointerException。

if(mMediaPlayer != null) {
mMediaPlayer.stop();
}

关于java - 应用程序在 android 中意外停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10471644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com