Question

I am making an app which receives time from the user, and makes the phone to go into the vibration mode at the scheduled time. The problem is that it throws a run time exception (null pointer exception), which says unable to start receiver for AlarmReciever.class. How do I resolve this?

code:

public class MainActivity extends Activity {

    private EditText mHours;
    private EditText mMinutes;
    private Calendar mCalendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mHours = (EditText)findViewById(R.id.editText1);
        mMinutes = (EditText)findViewById(R.id.editText2);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }   
    public void getdata(View V){

        String hours = mHours.getText().toString();
        String minutes = mMinutes.getText().toString();

        int hrs = Integer.parseInt(hours);
        int min = Integer.parseInt(minutes);

        mCalendar= Calendar.getInstance();
        mCalendar.set(Calendar.HOUR_OF_DAY, hrs );
        mCalendar.set(Calendar.MINUTE, min);


        Intent intentAlarm = new Intent();
        intentAlarm.setClass(this, AlarmReciever.class);


        AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        //set the alarm for particular time
        mAlarmManager.set(AlarmManager.RTC_WAKEUP,mCalendar.getTimeInMillis(), PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
        Toast.makeText(this, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();
        }
}

AlarmReciever.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.widget.Toast;

public class AlarmReciever extends BroadcastReceiver
{
private AudioManager mAudioManager;
     @Override
        public void onReceive(Context context, Intent intent)
        {
                // TODO Auto-generated method stub


                  // here you can start an activity or service depending on your need
                 // for ex you can start an activity to vibrate phone or to ring the phone   

        // mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
        mAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                // Show the toast  like in above screen shot
                Toast.makeText(context, "Alarm Set", Toast.LENGTH_LONG).show();
         }

}

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ish.k.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

 <!-- permission required to use Alarm Manager -->
        <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="ishan.khandelwal.test.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity 
        android:name="ishan.khandelwal.test.AlarmReciever"
        android:label="AlarmReciever">

    </activity>

     <!-- Register the Alarm Receiver -->
               <receiver android:name=".AlarmReciever"/>
</application>

</manifest>

my Logcat trace:

03-17 23:22:32.422: D/AndroidRuntime(13838): Shutting down VM
03-17 23:22:32.422: W/dalvikvm(13838): threadid=1: thread exiting with uncaught exception (group=0x40f11438)
03-17 23:22:32.472: E/AndroidRuntime(13838): FATAL EXCEPTION: main
03-17 23:22:32.472: E/AndroidRuntime(13838): java.lang.RuntimeException: Unable to start receiver ishan.khandelwal.test.AlarmReciever: java.lang.NullPointerException
03-17 23:22:32.472: E/AndroidRuntime(13838):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2291)
03-17 23:22:32.472: E/AndroidRuntime(13838):    at android.app.ActivityThread.access$1600(ActivityThread.java:143)
03-17 23:22:32.472: E/AndroidRuntime(13838):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
03-17 23:22:32.472: E/AndroidRuntime(13838):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 23:22:32.472: E/AndroidRuntime(13838):    at android.os.Looper.loop(Looper.java:137)
03-17 23:22:32.472: E/AndroidRuntime(13838):    at android.app.ActivityThread.main(ActivityThread.java:4960)
03-17 23:22:32.472: E/AndroidRuntime(13838):    at java.lang.reflect.Method.invokeNative(Native Method)
03-17 23:22:32.472: E/AndroidRuntime(13838):    at java.lang.reflect.Method.invoke(Method.java:511)
03-17 23:22:32.472: E/AndroidRuntime(13838):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-17 23:22:32.472: E/AndroidRuntime(13838):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-17 23:22:32.472: E/AndroidRuntime(13838):    at dalvik.system.NativeStart.main(Native Method)
03-17 23:22:32.472: E/AndroidRuntime(13838): Caused by: java.lang.NullPointerException
03-17 23:22:32.472: E/AndroidRuntime(13838):    at ish.k.test.AlarmReciever.onReceive(AlarmReciever.java:23)
03-17 23:22:32.472: E/AndroidRuntime(13838):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2284)
03-17 23:22:32.472: E/AndroidRuntime(13838):    ... 10 more
Was it helpful?

Solution

Your mAudioManager is null. You have only declared the variable but haven't initialized it. Use getSystemService(Context.AUDIO_SERVICE) on a valid Context to initialize it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top