Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7908969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:10:27+00:00 2026-06-03T12:10:27+00:00

My android app is keep giving me this error. The application has stopped working

  • 0

My android app is keep giving me this error. The application has stopped working unexpectedly. Please try again. I dont know what is the wrong with my code. I am working on this app for hours and now my head is blowing apart. What am I doing wrong in my code?

xml file

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

main class file

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

here is my service class

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

}

Edit

Here is my logcat

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

edit

debugging image

Logcat
This is latest logcat after if(mMediaPlayer != null) { mMediaPlay.stop() } but it still doesnt work.

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@
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:10:29+00:00Added an answer on June 3, 2026 at 12:10 pm

    You should look at this lines of the 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)
    

    You have a NullPointer at line 74 in the onReceive method of BatteryService.java

    Make sure that all the variables are initialized or handled properly.

    Edit: After looking a little more at the code, it is likely the problem at

    mMediaPlayer.stop();
    

    it can happen that mMediaPlayer not initialized so returning a NullPointerException.

    Edit 2:
    This should fix the NullPointerException.

    if(mMediaPlayer != null) {
      mMediaPlayer.stop();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an Android app which has a periodic background Service. I want this
This is the error my logcat is giving me. I understand it has something
I am using Eclipse to write my Android app and I keep getting this
Im designing my first android application and keep coming up with the following error
My android app has a two word app name, and the 2nd word doesn't
An android app I'm working on relies on a web page for most of
I'm writing an Android app to keep up with shopping lists. The main view
I'm doing what seems like simple math in an android app, however I keep
How can an android app keep sending data to server even it is closed
I cannot export nor build my android app because of an error in proguard.cfg.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.