java.lang.RuntimEexception:无法启动接收器Null指针异常

时间:2014-03-17 11:53:46

标签: android android-intent nullpointerexception alarmmanager runtimeexception

我正在创建一个从用户那里接收时间的应用程序,并使手机在预定时间进入振动模式。 问题是它抛出一个运行时异常(空指针异常),表示无法启动AlarmReciever.class的接收器。 我该如何解决这个问题?

代码:

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

}

清单:

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

我的Logcat追踪:

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

1 个答案:

答案 0 :(得分:1)

您的mAudioManagernull。您只声明了变量但尚未对其进行初始化。在有效getSystemService(Context.AUDIO_SERVICE)上使用Context进行初始化。