简体   繁体   中英

Android Service java.lang.IndexOutOfBoundsException

I am trying to run a service which would connect to a website and get specific data from the website to the android device sqlite datebase. It works fine. but as i do, it sometimes gives this error. but then again when ever the service runs again, it works well as usual. I simply cannot find what is the error. Plus, this is the first time im writing this much of a complex service and please let me know if there is any effective way of doing this.

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;
    }

}

The following is the logcat output for the error.

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)

Your List xx has 0 elements and you try to access the first one - this is your problem

Perhaps you run into this IOException in check:

➜  ~  curl http://www.xoxoxoxoxox.com/index.php
curl: (6) Could not resolve host: www.xoxoxoxoxox.com

You never add an element to your ArrayList xx . So the list is empty in the match function.

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();
}

}

Use above code and check

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM