2 qq 28818163 qq_28818163 于 2015.06.12 10:47 提问

程序运行时强制关闭,可能为空指针

日志: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();

}

6个回答

liuxian13183
liuxian13183   2015.06.14 21:00
已采纳

String content=sp.getString(KEY_ALARM_LIST, null);

if (content!=null) { String[] timeString=content.split(",");
你的代码首先不是空指针异常,而是值类型异常。那么比较可能是,首先share里没有值,可是你默认取出的是null还不是"",所以你的content值可能是null,接下来content来跟空比较,首次肯定是要出错的。

danielinbiti
danielinbiti   Ds   Rxr 2015.06.12 10:58
 if (content!=null) {
String[] timeString=content.split(",");

for (String string : timeString) {//看看timeString是什么
adapter.add(new AlarmData(Long.parseLong(string)));
}
可以加个判断
for (String string : timeString) {//看看timeString是什么
  if(string!=null&&string.trim().length>0){//看看是不是有空串,另外看看string是否有非数字的存在
    adapter.add(new AlarmData(Long.parseLong(string)));
}
}
qq_25099755
qq_25099755   2015.06.12 17:23

java.lang.NumberFormatException 数字格式化异常,很有可能是这句adapter.add(new AlarmData(Long.parseLong(string))); 抛出的异常,
仔细检查一下你的String数组里面的内容

u010866453
u010866453   2015.06.12 21:28

最好搞一个txt发过来 这有点多

u013370108
u013370108   2015.06.12 23:15

打一下断点,运行确定每一项传回来的参数,确定谁是空值

xby1993
xby1993   2015.06.13 03:05

java.lang.NumberFormatException

Csdn user default icon
上传中...
上传图片
插入图片