如何在Asynctask

时间:2018-02-04 11:32:20

标签: android sqlite google-maps android-asynctask

我有这个代码

public  ArrayList<StationData> getAllStations(String num1){

    ArrayList<StationData> stationData = new ArrayList<>();


    Cursor cursor = mydb.rawQuery(" select * from   Stations where lines like '%"+num1+"%'   ORDER BY id DESC", null);

    if (cursor.moveToFirst()){
        do {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            char faName =  (char)(cursor.getColumnIndex("name"));
            char enName = (char) (cursor.getColumnIndex("enName"));
            char lines = (char)(cursor.getColumnIndex("lines"));
            char address = (char) (cursor.getColumnIndex("address"));
            float latitude = cursor.getFloat(cursor.getColumnIndex("latitude"));
            float longitude = cursor.getFloat(cursor.getColumnIndex("longitude"));
            StationData hh = new StationData(id, faName, enName, lines, address, latitude, longitude);

            stationData.add(hh);

        } while (cursor.moveToNext());
        cursor.close();

    }else{

    }
    return stationData;
}

这是我的AsyncTask

  class MetroLineMaker extends AsyncTask<String,Void,String>{

        MetroDataBase metroDataBase = new MetroDataBase(getApplicationContext());
        ArrayList<StationData> metroData;
        StationData stationData = new StationData();
//        metroData = new ArrayList<>();

        protected void onPreExecute() {

        }

        @Override
        protected String doInBackground(String... strings) {


            metroData = metroDataBase.getAllStations(strings[0]);

            for (int i=0;i<metroData.size()-1 ;i++) {

                LatLng last = new LatLng(metroData.get(i).getLatitude(), metroData.get(i).getLongitude());
                LatLng next = new LatLng(metroData.get(i + 1).getLatitude(), metroData.get(i + 1).getLongitude());

//                    createMarker(metroData.get(i).getLatitude(), metroData.get(i).getLongitude());


                mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(metroData.get(i).getLatitude(), metroData.get(i).getLongitude()))
                        .anchor(0.5f, 0.5f)
                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_metro_line_one)));


                mMap.addPolyline(
                        new PolylineOptions().add(
                                last, next
                        ).width(10).color(Color.RED)
                );


            }


            return null;
        }

        protected void onPostExecute(String result) {

        }
    }

我有这个错误

  

E / AndroidRuntime:致命异常:AsyncTask#1                     过程:com.arnaway.metromap,PID:9493                     java.lang.RuntimeException:执行doInBackground()时发生错误                         在android.os.AsyncTask $ 3.done(AsyncTask.java:353)                         在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)                         at java.util.concurrent.FutureTask.setException(FutureTask.java:252)                         在java.util.concurrent.FutureTask.run(FutureTask.java:271)                         在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:245)                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)                         at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:636)                         在java.lang.Thread.run(Thread.java:764)                      引起:java.lang.ArrayIndexOutOfBoundsException:length = 0;索引= 0                         在com.arnaway.metromap.MainActivity $ MetroLineMaker.doInBackground(MainActivity.java:1028)                         在com.arnaway.metromap.MainActivity $ MetroLineMaker.doInBackground(MainActivity.java:1013)                         在android.os.AsyncTask $ 2.call(AsyncTask.java:333)                         at java.util.concurrent.FutureTask.run(FutureTask.java:266)                         在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:245)                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)                         at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:636)                         在java.lang.Thread.run(Thread.java:764)

如何在AsyncTask中使用我的数据库并在MainActivity中显示我的标记我的AsyncTask也在MainActivity中。

1 个答案:

答案 0 :(得分:0)

我没有数据库方面的经验,但活动中的其他代码在哪里?你在哪里申报mydb?看起来你的数组列表中没有填充metroData,因此它给出了数组越界错误。

您的getAllStations方法在哪里?在metroDataBase类中?那么这个类的代码在哪里?