[英]Android Service java.lang.IndexOutOfBoundsException
我正在嘗試運行將連接到網站並從網站獲取特定數據到android設備sqlite日期庫的服務。 工作正常。 但正如我所做的那樣,有時會出現此錯誤。 但是再一次,只要該服務再次運行,它就會照常運行。 我根本找不到錯誤。 另外,這是我第一次編寫這么多復雜的服務,如果有任何有效的方法,請讓我知道。
public class Serviceclass extends Service {
protected static final String MyPREFERENCES = null;
private boolean isRunning;
private Context context;
private Thread backgroundThread;
private float n;
private ArrayList<String> xx = new ArrayList<String>();
private Document doc;
private String checkdate;
DatabaseHandler db = new DatabaseHandler(this);
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
this.context = this;
this.isRunning = false;
this.backgroundThread = new Thread(myTask);
}
private Runnable myTask = new Runnable() {
public void run() {
SharedPreferences myPrefs = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String date = myPrefs.getString("Date", null);
if (date != null) {
System.out.println("its not empty");
// check if the dates are a match
match(date);
} else {
System.out.println("its empty");
update();
}
stopSelf();
}
};
@Override
public void onDestroy() {
this.isRunning = false;
}
protected void match(String date) {
check();
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
checkdate = sharedpreferences.getString("Date", null);
System.out.println(checkdate);
if (date.contentEquals(checkdate)) {
System.out.println("Dates are the same");
db.addDaily(new Daily(xx.get(0), xx.get(7), xx.get(8)));
} else {
System.out.println("Dates are not the same");
update();
}
}
private void check() {
// TODO Auto-generated method stub
try {
doc = Jsoup.connect("http://www.xoxoxoxoxox.com/index.php").get();
Log.i("tag", "post");
Elements myin = doc.getElementsByClass("rates_text_home");
for (Element element : myin) {
String a = element.text();
xx.add(a);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void update() {
check();
Log.i("tag", xx.get(0));
System.out.println("Loading Data");
db.addDaily(new Daily(xx.get(0), xx.get(7), xx.get(8)));
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("Date", xx.get(0));
editor.commit();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!this.isRunning) {
this.isRunning = true;
this.backgroundThread.start();
}
return START_STICKY;
}
}
以下是該錯誤的logcat輸出。
FATAL EXCEPTION: Thread-13
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
at java.util.ArrayList.get(ArrayList.java:311)
at com.example.marketapp.Serviceclass.match(Serviceclass.java:82)
at com.example.marketapp.Serviceclass$1.run(Serviceclass.java:53)
at java.lang.Thread.run(Thread.java:1019)
您的列表xx包含0個元素,而您嘗試訪問第一個元素-這是您的問題
也許您在檢查中遇到了這個IOException:
➜ ~ curl http://www.xoxoxoxoxox.com/index.php
curl: (6) Could not resolve host: www.xoxoxoxoxox.com
您永遠不會將元素添加到ArrayList xx
。 因此,匹配功能中的列表為空。
protected void update() {
check();
if(xx.size() > 0){
Log.i("tag", xx.get(0));
System.out.println("Loading Data");
db.addDaily(new Daily(xx.get(0), xx.get(7), xx.get(8)));
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("Date", xx.get(0));
editor.commit();
}
}
使用上面的代碼並檢查
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.