日志:java.lang.RuntimeException: Unable to start activity ComponentInfo{pcAlarm.pack/pcAlarm.pack.PCAlarmActivity}: java.lang.NumberFormatException: 空指針錯誤,可能在
private void readSaveAlarm(){
SharedPreferences sp =getContext().getSharedPreferences(PCAlarmActivity.class.getName(), Context.MODE_PRIVATE);
String content=sp.getString(KEY_ALARM_LIST, null);
if (content!=null) {
String[] timeString=content.split(",");
for (String string : timeString) {
adapter.add(new AlarmData(Long.parseLong(string)));
}
}
}方法裡
package pcAlarm.pack;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Currency;
import java.util.List;
import pcAlarm.pack.PCAlarmActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent.OnFinished;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.provider.Contacts;
import android.view.View;
import android.widget.ArrayAdapter;
import pcAlarm.pack.AlarmView;
import pcAlarm.pack.AlarmView.AlarmData;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TimePicker;
public class PCAlarmActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tabAlarm").setIndicator("鬧鐘").setContent(R.id.tabAlarm));
btnAddAlarm=(Button)findViewById(R.id.btnAddAlarm);
lvListView=(ListView)findViewById(R.id.lvListView);
adapter=new ArrayAdapter<PCAlarmActivity.AlarmData>(getContext(), android.R.layout.simple_list_item_1);
lvListView.setAdapter(adapter);
adapter.add(new AlarmData(System.currentTimeMillis()));
readSaveAlarm();
btnAddAlarm.setOnClickListener(new OnClickListener() {
private Calendar mcalender;
@Override
public void onClick(View v) {
Calendar.getInstance().setTimeInMillis(System.currentTimeMillis());
mcalender = Calendar.getInstance();
int mHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int mMinute = Calendar.getInstance().get(Calendar.MINUTE);
new TimePickerDialog(PCAlarmActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
Calendar.getInstance().setTimeInMillis(System.currentTimeMillis());
Calendar.getInstance().set(Calendar.HOUR_OF_DAY, hourOfDay);
Calendar.getInstance().set(Calendar.MINUTE, minute);
Calendar.getInstance().set(Calendar.SECOND, 0);
Calendar.getInstance().set(Calendar.MILLISECOND, 0);
AlarmData add = new AlarmData(Calendar.getInstance().getTimeInMillis());
saveAlarmList();
}
}, mHour, mMinute, true).show();
}
});
lvListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View arg1,
final int position, long id) {
new AlertDialog.Builder(getContext()).setTitle("操作選項").setItems(new CharSequence[]{"刪除"}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
deleteAlarm(position);
break;
default:
break;
}
}
}).setNegativeButton("取消", null).show();
return true;
}
});
}
private void deleteAlarm(int position){
adapter.remove(adapter.getItem(position));
}
private void saveAlarmList(){
Editor editor=getContext().getSharedPreferences(PCAlarmActivity.class.getCanonicalName(), Context.MODE_PRIVATE).edit();
StringBuffer sb=new StringBuffer();
for (int i = 0; i < adapter.getCount(); i ) {
sb.append(adapter.getItem(i).getTime()).append(",");
}
String content = sb.toString().substring(0, sb.length()-1);
editor.putString(KEY_ALARM_LIST, sb.toString().substring(0, sb.length()));
System.out.println();
editor.commit();
}
String content=sp.getString(KEY_ALARM_LIST, null);
if (content!=null) { String[] timeString=content.split(",");
你的代碼首先不是空指針異常,而是值類型異常。那麼比較可能是,首先share裡沒有值,可是你默認取出的是null還不是"",所以你的content值可能是null,接下來content來跟空比較,首次肯定是要出錯的。