package com.s5c.Changlink;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;
@SuppressLint({ "SimpleDateFormat", "HandlerLeak" })
public class sqlService extends Service {
SharedPreferences sp;
String branch,mold,sql,id,Changelocation;
Thread Change;
Timer timer;
Date molds;
Date data=new Date(System.currentTimeMillis());
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Message message;
public void onCreate() {
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
sp =getSharedPreferences("User", MODE_PRIVATE);
branch = sp.getString("branch", "");
mold = sp.getString("mold", "");
try {
molds = dateFormater.parse(mold);
} catch (ParseException e) {
e.printStackTrace();
}
if(molds.before(data)){
timer.schedule(change, molds, 300000);////代碼在這裡出錯,使程序崩潰
}else{
timer.schedule(change, 300000, 300000);
}
return super.onStartCommand(intent, flags, startId);
}
TimerTask change = new TimerTask(){
public void run() {
message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Toast.makeText(sqlService.this, "完全正常", Toast.LENGTH_LONG).show();
break;
}
super.handleMessage(msg);
}
};
public void onDestroy() {
Toast.makeText(this, "您將取消服務", Toast.LENGTH_LONG).show();
super.onDestroy();
}
public IBinder onBind(Intent intent) {
return null;
}
}
以上就是程序代碼,我在造成崩潰的地方有標注,不明白為什麼會造成程序崩潰
你的Timer貌似沒有創建。。只看到聲明,沒見到new