如果我称这种方法
(变量声明 public static SharedPreferences prefs = null;)
public void setManualycallResetCal(long time)
{
Preference calPrefCal = findPreference("callReset");
callResetCal.setTimeInMillis(time);
Date d = new Date(time);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
StringBuilder sumValue = new StringBuilder("Since :");
sumValue.append(simpleDateFormat.format(d));
calPrefCal.setSummary( sumValue.toString() );
edit = prefs.edit();
edit.putString("calDate", sumValue.toString() );
edit.commit();
}
应用程序与空指针崩溃 有线或什么
错误:
calPrefCal.setSummary( sumValue.toString() );
我的通话代码
if (Settings.prefs != null) {
Calendar cal = Settings.callResetCal;
Calendar comp = Calendar.getInstance();
if (cal.getTimeInMillis() < comp.getTimeInMillis()) {
int reset = Settings.callReset;
Calendar end = Calendar.getInstance();
end.setTimeInMillis(cal.getTimeInMillis());
end.add(Calendar.DATE, reset);
new Settings().setManualycallResetCal(end.getTimeInMillis());
} // end if
} // end if null check
如果我评论整个调用代码块,一切正常
整页代码
package srinath.logger.app;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;
public class Settings extends PreferenceActivity {
private OnSharedPreferenceChangeListener listener;
public static SharedPreferences prefs = null;
static SharedPreferences.Editor edit = null;
public static boolean callEnable;
public static boolean callNotify;
public static int callUpdate;
public static int callReset; // sec
// since
public static Calendar callResetCal;
public static int callUnits;
public static int callLimit;
//public static int unitPulse; // cost per unit of extra usage
public static boolean dataEnable;
public static boolean dataNotify;
public static int dataUpdate;
public static int dataReset; // sec
public static Calendar dataResetCal;
public static long dataUnits;
public static long dataLimit;
String dummy = "";
public Settings() {
// TODO Auto-generated constructor stub
}
public void setManualycallResetCal(long time)
{
//
Preference calPrefCal = findPreference("callReset");
callResetCal.setTimeInMillis(time);
Date d = new Date(time);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
StringBuilder sumValue = new StringBuilder("Since :");
sumValue.append(simpleDateFormat.format(d));
calPrefCal.setSummary( sumValue.toString() );
prefs = Settings.this.getSharedPreferences("Prefs_Name",
MODE_WORLD_READABLE);
edit = prefs.edit();
edit.putString("calDate", sumValue.toString() );
edit.commit();
edit = prefs.edit();
edit.putString("calDate", sumValue.toString() );
edit.commit();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
Preference calPrefCal = findPreference("callReset");
// set defaults to class variable
// array list stores in string format .. seems getInt cant parse
// call
callEnable = prefs.getBoolean("callEnable", false);
//Log.d("callEnable","" + callEnable);
callNotify = prefs.getBoolean("callNotify", false);
//Log.d("notify","" + callNotify);
dummy = prefs.getString("callUpdate", "0");
callUpdate = Integer.parseInt(dummy);
//Log.d("callUpdate","" + callUpdate);
dummy = prefs.getString("callReset", "0");
callReset = Integer.parseInt(dummy);
//Log.d("callReset","" + callReset);
callResetCal = Calendar.getInstance();
if(prefs.getString("calDate", "0").equals("0")) // no pref with name calDate
{
Calendar cMain = Calendar.getInstance();
cMain.setTimeInMillis(0);
cMain.set(Calendar.DATE, callResetCal.get(Calendar.DATE));
cMain.set(Calendar.MONTH, callResetCal.get(Calendar.MONTH));
cMain.set(Calendar.YEAR, callResetCal.get(Calendar.YEAR));
// set since
callResetCal.setTimeInMillis(cMain.getTimeInMillis());
// to print create date
Date d = new Date(cMain.getTimeInMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
StringBuilder sumValue = new StringBuilder("Since :");
sumValue.append(simpleDateFormat.format(d));
calPrefCal.setSummary( sumValue.toString() );
edit = prefs.edit();
edit.putString("calDate", sumValue.toString() );
edit.commit();
} // else set summary as of pref
else
{
calPrefCal.setSummary(prefs.getString("calDate", "0"));
}
dummy = prefs.getString("callUnits", "0");
callUnits = Integer.parseInt(dummy);
//Toast toast = Toast.makeText(getApplicationContext(), "" + dummy , Toast.LENGTH_SHORT);
//toast.show();
//Log.d("callUnits","" + callUnits);
dummy = prefs.getString("callLimit", "0");
callLimit = Integer.parseInt(dummy);
//Log.d("callLimit","" + callLimit);
// data
dataEnable = prefs.getBoolean("dataEnable", false);
dataNotify = prefs.getBoolean("dataNotify", false);
dummy = prefs.getString("dataUpdate", "0");
dataUpdate = Integer.parseInt(dummy);
dummy = prefs.getString("dataReset", "0");
dataReset = Integer.parseInt(dummy);
dummy = prefs.getString("dataUnits", "0");
dataUnits = Long.parseLong(dummy);
dummy = prefs.getString("dataLimit", "0");
dataLimit = Long.parseLong(dummy);
// end
// preference onChange listener
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
//DEBUGGING ONLY
Toast toast = Toast.makeText(getApplicationContext(), "preferenceUpdate:DEBUG_ONLY", Toast.LENGTH_SHORT);
toast.show();
if(key.equals("callEnable"))
{
callEnable = prefs.getBoolean(key, false);
}
else if(key.equals("callNotify"))
{
callNotify = prefs.getBoolean(key, false);
}
else if(key.equals("callUpdate"))
{
dummy = prefs.getString(key, "0" );
callUpdate = Integer.parseInt(dummy);
}
else if(key.equals("callReset"))
{
Preference calPrefCal = findPreference(key);
dummy = prefs.getString(key, "0" );
callReset = Integer.parseInt(dummy);
callResetCal = Calendar.getInstance();
Calendar cMain = Calendar.getInstance();
cMain.setTimeInMillis(0);
cMain.set(Calendar.DATE, callResetCal.get(Calendar.DATE));
cMain.set(Calendar.MONTH, callResetCal.get(Calendar.MONTH));
cMain.set(Calendar.YEAR, callResetCal.get(Calendar.YEAR));
callResetCal.setTimeInMillis(cMain.getTimeInMillis());
// to print create date
Date d = new Date(cMain.getTimeInMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
StringBuilder sumValue = new StringBuilder("Since :");
sumValue.append(simpleDateFormat.format(d));
calPrefCal.setSummary( sumValue.toString() );
edit = prefs.edit();
edit.putString("calDate", sumValue.toString() );
edit.commit();
}
else if(key.equals("callUnits"))
{
dummy = prefs.getString(key, "0" );
callUnits = Integer.parseInt(dummy);
}
else if(key.equals("callLimit"))
{
dummy = prefs.getString(key, "0" );
callLimit = Integer.parseInt(dummy);
}
// data
else if(key.equals("dataEnable"))
{
dataEnable = prefs.getBoolean(key, false);
}
else if(key.equals("dataNotify"))
{
dataNotify = prefs.getBoolean(key, false);
}
else if(key.equals("dataUpdate"))
{
dummy = prefs.getString(key, "0" );
dataUpdate = Integer.parseInt(dummy);
}
else if(key.equals("dataReset"))
{
dummy = prefs.getString(key, "0" );
dataReset = Integer.parseInt(dummy);
dataResetCal = Calendar.getInstance();
}
else if(key.equals("dataUnits"))
{
dummy = prefs.getString(key, "0" );
dataUnits = Long.parseLong(dummy);
}
else if(key.equals("dataLimit"))
{
dummy = prefs.getString(key, "0" );
dataLimit = Long.parseLong(dummy);
}
} // end of
}; // end of
prefs.registerOnSharedPreferenceChangeListener(listener);
}
}