我下载闹钟应用,因为我需要看看它是如何工作的。 GNU许可证。
但我无法运行它。当我点击(某事?)时,我收到了这个错误:
08-03 14:40:26.154: E/AndroidRuntime(255): at com.codeskraps.talarkam.TalarkamActivity.onResume(TalarkamActivity.java:108)
部分代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate Started");
setContentView(R.layout.alarm_controller);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
tpAlarm = (TimePicker) findViewById(R.id.tpAlarm);
chkSnooze = (CheckBox) findViewById(R.id.chkSnooze);
etxtSnoozeMin = (EditText) findViewById(R.id.etxtSnoozeMin);
Button button = (Button) findViewById(R.id.btnSetAlarm);
btnSetTone = (Button) findViewById(R.id.btnSetTone);
chkVolume = (CheckBox) findViewById(R.id.chkVolume);
skbVolume = (SeekBar) findViewById(R.id.skbVolume);
button.setOnClickListener(setAlarmListener);
btnSetTone.setOnClickListener(this);
}
protected void onResume() {
Log.d(TAG, "onResume");
super.onResume();
tpAlarm.setCurrentHour(prefs.getInt(M_HOUR, 0));
tpAlarm.setCurrentMinute(prefs.getInt(M_MINT, 0));
chkSnooze.setChecked(prefs.getBoolean(M_SNOOZE, false));
etxtSnoozeMin.setText(prefs.getString(SNOOZE_T, "0"));
chkVolume.setChecked(prefs.getBoolean(CHKVOLUME, false));
skbVolume.setProgress(prefs.getInt(SKBVOLUME, 0));
Uri alarmUri = Uri.parse(prefs.getString(TONE, null));
Ringtone ringtone = RingtoneManager.getRingtone(this, alarmUri);
if (ringtone != null) {
String name = ringtone.getTitle(this);
btnSetTone.setText(name);
} else
btnSetTone.setText(getString(R.string.btn_SetAlarmTone));
if (prefs.getBoolean(FIRST_LAUNCH, true)) {
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(FIRST_LAUNCH, false);
editor.commit();
}
}
protected void onPause() {
Log.d(TAG, "onPause");
super.onPause();
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(M_HOUR, tpAlarm.getCurrentHour());
editor.putInt(M_MINT, tpAlarm.getCurrentMinute());
editor.putBoolean(M_SNOOZE, chkSnooze.isChecked());
editor.putString(SNOOZE_T, etxtSnoozeMin.getText().toString());
editor.putBoolean(CHKVOLUME, chkVolume.isChecked());
editor.putInt(SKBVOLUME, skbVolume.getProgress());
editor.commit();
}
logcat的:
08-03 14:40:25.684: D/TalarkamActivity(255): onCreate Started
08-03 14:40:25.965: D/dalvikvm(255): GC freed 599 objects / 51056 bytes in 86ms
08-03 14:40:26.134: D/TalarkamActivity(255): onResume
08-03 14:40:26.144: D/AndroidRuntime(255): Shutting down VM
08-03 14:40:26.144: W/dalvikvm(255): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
08-03 14:40:26.144: E/AndroidRuntime(255): Uncaught handler: thread main exiting due to uncaught exception
08-03 14:40:26.154: E/AndroidRuntime(255): java.lang.RuntimeException: Unable to resume activity {com.codeskraps.talarkam/com.codeskraps.talarkam.TalarkamActivity}: java.lang.NullPointerException: uriString
08-03 14:40:26.154: E/AndroidRuntime(255): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2950)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2965)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2516)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.os.Looper.loop(Looper.java:123)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-03 14:40:26.154: E/AndroidRuntime(255): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 14:40:26.154: E/AndroidRuntime(255): at java.lang.reflect.Method.invoke(Method.java:521)
08-03 14:40:26.154: E/AndroidRuntime(255): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-03 14:40:26.154: E/AndroidRuntime(255): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-03 14:40:26.154: E/AndroidRuntime(255): at dalvik.system.NativeStart.main(Native Method)
08-03 14:40:26.154: E/AndroidRuntime(255): Caused by: java.lang.NullPointerException: uriString
08-03 14:40:26.154: E/AndroidRuntime(255): at android.net.Uri$StringUri.<init>(Uri.java:420)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.net.Uri$StringUri.<init>(Uri.java:410)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.net.Uri.parse(Uri.java:382)
08-03 14:40:26.154: E/AndroidRuntime(255): at com.codeskraps.talarkam.TalarkamActivity.onResume(TalarkamActivity.java:108)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.app.Activity.performResume(Activity.java:3763)
08-03 14:40:26.154: E/AndroidRuntime(255): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2937)
答案 0 :(得分:0)
错误出现在onResume()
:
Uri alarmUri = Uri.parse(prefs.getString(TONE, null));
如果此首选项不存在,则默认为null
,这会导致NullPointerException。
编辑添加了代码建议以摆脱NPE
在onResume()
:
Uri alarmUri = Uri.parse(prefs.getString(TONE, null));
Ringtone ringtone = RingtoneManager.getRingtone(this, alarmUri);
if (ringtone != null) {
String name = ringtone.getTitle(this);
btnSetTone.setText(name);
} else
btnSetTone.setText(getString(R.string.btn_SetAlarmTone));
使用此代码:
Ringtone ringtone = null;
String toneString = prefs.getString(TONE, null);
if (toneString != null) {
Uri alarmUri = Uri.parse(toneString);
ringtone = RingtoneManager.getRingtone(this, alarmUri);
}
if (ringtone != null) {
String name = ringtone.getTitle(this);
btnSetTone.setText(name);
} else {
btnSetTone.setText(getString(R.string.btn_SetAlarmTone));
}
答案 1 :(得分:0)
由于你还没有发布整个代码,因此弄清楚它失败的线路是否很棘手(第108行?)。但我的猜测是以下行是根本原因:
Uri alarmUri = Uri.parse(prefs.getString(TONE,null));
在prefs中找不到TONE键/对象,并将默认值设置为null。