带声音的报警对话框

时间:2015-03-07 21:06:54

标签: java android

倒数计时器结束时,我正在弹出警报对话框。它运行正常,但是当我尝试在对话框中实现声音时,它无法正常工作。当我在铃声列表中加载用户从应用程序中的其他位置选择的警报(使用共享的pref号码)时,我收到NullPointerException。如果我只是输入一个数字而不使用SharedPreferences,那么当我将数据源设置为MediaPlayer时,我会得到一个NullPointerException。这是我的代码:

public class AlarmDialog extends DialogFragment {

MediaPlayer mp;
Context context;
Uri alarmSound;
int alarmChosen;
SharedPreferences sharedPrefs;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    LINE 31 sharedPrefs = context.getSharedPreferences("AlarmChosen", Context.MODE_PRIVATE);
    alarmChosen = sharedPrefs.getInt("AlarmChosen", 1);
    RingtoneManager ringtoneMgr = new RingtoneManager(context);
    ringtoneMgr.setType(RingtoneManager.TYPE_ALL);
    alarmSound = ringtoneMgr.getRingtoneUri(alarmChosen);       

    mp = new MediaPlayer();
    mp.reset();
    try {
        LINE 41 mp.setDataSource(alarmSound.toString());
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        mp.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mp.start();

    getActivity().getWindow().addFlags(
            LayoutParams.FLAG_TURN_SCREEN_ON
                | LayoutParams.FLAG_DISMISS_KEYGUARD);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Alarm");
    builder.setMessage("An Alarm by AlarmManager");

    builder.setPositiveButton("OK", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            mp.stop();
            mp.release();
            getActivity().finish();
        }
    });

    return builder.create();
}   
}

我该如何解决这个问题?

编辑: 来自sharedPrefs的LogCat错误:

03-07 22:22:46.589: E/AndroidRuntime(28062): FATAL EXCEPTION: main
03-07 22:22:46.589: E/AndroidRuntime(28062): Process: com.adrissa.kitchentimer, PID: 28062
03-07 22:22:46.589: E/AndroidRuntime(28062): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adrissa.kitchentimer/com.adrissa.kitchentimer.DemoActivity}: java.lang.NullPointerException
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.app.ActivityThread.access$900(ActivityThread.java:172)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.os.Looper.loop(Looper.java:136)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.app.ActivityThread.main(ActivityThread.java:5586)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at java.lang.reflect.Method.invokeNative(Native Method)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at java.lang.reflect.Method.invoke(Method.java:515)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at dalvik.system.NativeStart.main(Native Method)
03-07 22:22:46.589: E/AndroidRuntime(28062): Caused by: java.lang.NullPointerException
03-07 22:22:46.589: E/AndroidRuntime(28062):    at com.adrissa.kitchentimer.AlarmDialog.onCreateDialog(AlarmDialog.java:31)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1177)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.app.Activity.performStart(Activity.java:5461)
03-07 22:22:46.589: E/AndroidRuntime(28062):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2367)
03-07 22:22:46.589: E/AndroidRuntime(28062):    ... 11 more

LogCat from error,当我只是插入0而不是在sharedPrefs中选择的数字时。

03-07 22:31:30.979: E/AndroidRuntime(30527): FATAL EXCEPTION: main
03-07 22:31:30.979: E/AndroidRuntime(30527): Process: com.adrissa.kitchentimer, PID: 30527
03-07 22:31:30.979: E/AndroidRuntime(30527): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adrissa.kitchentimer/com.adrissa.kitchentimer.DemoActivity}: java.lang.NullPointerException
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.app.ActivityThread.access$900(ActivityThread.java:172)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.os.Looper.loop(Looper.java:136)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.app.ActivityThread.main(ActivityThread.java:5586)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at java.lang.reflect.Method.invokeNative(Native Method)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at java.lang.reflect.Method.invoke(Method.java:515)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at dalvik.system.NativeStart.main(Native Method)
03-07 22:31:30.979: E/AndroidRuntime(30527): Caused by: java.lang.NullPointerException
03-07 22:31:30.979: E/AndroidRuntime(30527):    at com.adrissa.kitchentimer.AlarmDialog.onCreateDialog(AlarmDialog.java:41)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:295)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1177)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.app.Activity.performStart(Activity.java:5461)
03-07 22:31:30.979: E/AndroidRuntime(30527):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2367)
03-07 22:31:30.979: E/AndroidRuntime(30527):    ... 11 more

1 个答案:

答案 0 :(得分:1)

在第31行,您没有实例化上下文,因此它为null。尝试:

sharedPrefs = getActivity().getSharedPreferences("AlarmChosen", Context.MODE_PRIVATE);

增加: 对于第41行的错误,我建议使用更紧凑的模式来实现您想要做的事情。尝试:

alarmSound = ringtoneMgr.getRingtoneUri(alarmChosen);   
Ringtone r = RingtoneManager.getRingtone(getActivity(), alarmSound);
r.play();

如果您要做的只是播放所选铃声的声音,那么这可能是您最好的选择,除非您从getRingtoneUri检索到的Uri为空,否则不应该给您任何错误,这意味着alarmChosen不是此方法的有效参数...