0
我正在使用一個類別&界面來計算/更新日期&時間。在Android OS版本2.2及更高版本上更新背景上的日期和時間時出現異常
接口:
public interface CListener {
public void updateClock(String date, String time);
}
類:
public class CManager {
private CListener mListener = null;
private static Timer mTimer = null;
private static String mDateData = "";
private static String mTimeData = "";
private static CManager _instance = null;
private CManager() {
if (mTimer == null) {
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
updateTime();
}
}, 0, 1000);
}
}
public static CManager getInstance() {
if(_instance == null) {
_instance = new CManager();
}
return _instance;
}
public void setClockListener(ClockListener listener) {
this.mListener = listener;
}
public void updateTime() {
Date d = new Date();
// Calculating the date , month ,day ,hr ,min from Date d
mListener.updateClock(mDateData, mTimeData);
// mDateData -> Calculated combined Date
// mTimeData -> Calculated combined time
}
public String getDate() {
return mDateData;
}
public String getTime() {
return mTimeData;
}
}
我從一個活動
public class SampleActivity extends Activity{
private CManager mCManager = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCManager = CManager.getInstance();
}
調用CManager類0
上述代碼工作正常&更新日期&時間與Android操作系統版本1.6到2.1。
但是,相同的代碼給Android 2.2版的以下錯誤。上面的&。
04-08 03:57:39.269: ERROR/AndroidRuntime(419): FATAL EXCEPTION: Timer-0
04-08 03:57:39.269: ERROR/AndroidRuntime(419): java.lang.NullPointerException
04-08 03:57:39.269: ERROR/AndroidRuntime(419): at com.app.CManager.updateTime(CManager.java:144)
04-08 03:57:39.269: ERROR/AndroidRuntime(419): at com.app.CManager$1.run(CManager.java:54)
04-08 03:57:39.269: ERROR/AndroidRuntime(419): at java.util.Timer$TimerImpl.run(Timer.java:284)
04-08 03:57:39.301: WARN/ActivityManager(70): Force finishing activity com.app/.SampleActivity
問題的可能原因是什麼? 是否有任何替代/解決此問題。
請提供您的輸入/示例代碼。
在此先感謝。
親切問候,
CB