Java IndexOutOfBound异常在Android中使用arraylist

Java IndexOutOfBound异常在Android中使用arraylist,第1张

概述我是android开发的新手.我在这段代码中使用了一个名为Donors的用户定义数组列表,但它给了我错误.我多次看到这里发布了相同的错误,但仍然找不到我的解决方案.请帮忙.谢谢05-0123:45:10.8551756-3399/com.donateblood.blooddonationE/AndroidRuntime:FATALEXCEPTION:AsyncTa

我是android开发的新手.我在这段代码中使用了一个名为Donors的用户定义数组列表,但它给了我错误.我多次看到这里发布了相同的错误,但仍然找不到我的解决方案.请帮忙.谢谢

05-01 23:45:10.855 1756-3399/com.donateblood.blooddonation E/AndroIDRuntime: FATAL EXCEPTION: AsyncTask #2                                                                             Process: com.donateblood.blooddonation, PID: 1756                                                                             java.lang.RuntimeException: An error occured while executing doInBackground()                                                                                 at androID.os.AsyncTask.done(AsyncTask.java:300)                                                                                 at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)                                                                                 at java.util.concurrent.FutureTask.setException(FutureTask.java:222)                                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:242)                                                                                 at androID.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)                                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)                                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)                                                                                 at java.lang.Thread.run(Thread.java:818)                                                                              Caused by: java.lang.indexoutofboundsexception: InvalID index 0, size is 0                                                                                 at java.util.ArrayList.throwindexoutofboundsexception(ArrayList.java:255)                                                                                 at java.util.ArrayList.get(ArrayList.java:308)                                                                                 at com.donateblood.blooddonation.MainGUI.getotherLatLong(MainGUI.java:136)                                                                                 at com.donateblood.blooddonation.MainGUI$dbAsync.doInBackground(MainGUI.java:89)                                                                                 at com.donateblood.blooddonation.MainGUI$dbAsync.doInBackground(MainGUI.java:84)                                                                                 at androID.os.AsyncTask.call(AsyncTask.java:288)                                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:237)                                                                                 at androID.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)                                                                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)                                                                                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)                                                                                  at java.lang.Thread.run(Thread.java:818) 05-01 23:45:11.525 1756-1756/com.donateblood.blooddonation E/WindowManager: androID.vIEw.WindowLeaked: Activity com.donateblood.blooddonation.MainGUI has leaked window com.androID.internal.policy.impl.PhoneWindow$DecorVIEw{28ea9195 V.E..... R......D 0,0-1026,348} that was originally added here                                                                                at androID.vIEw.VIEwRootImpl.<init>(VIEwRootImpl.java:382)                                                                                at androID.vIEw.WindowManagerGlobal.addVIEw(WindowManagerGlobal.java:261)                                                                                at androID.vIEw.WindowManagerImpl.addVIEw(WindowManagerImpl.java:69)                                                                                at androID.app.Dialog.show(Dialog.java:298)                                                                                at com.donateblood.blooddonation.MainGUI$dbAsync.onPreExecute(MainGUI.java:100)                                                                                at androID.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)                                                                                at androID.os.AsyncTask.execute(AsyncTask.java:535)                                                                                at com.donateblood.blooddonation.MainGUI.onClick(MainGUI.java:63)                                                                                at androID.vIEw.VIEw.performClick(VIEw.java:4785)                                                                                at androID.vIEw.VIEw$PerformClick.run(VIEw.java:19858)                                                                                at androID.os.Handler.handleCallback(Handler.java:739)                                                                                at androID.os.Handler.dispatchMessage(Handler.java:95)                                                                                at androID.os.Looper.loop(Looper.java:155)                                                                                at androID.app.ActivityThread.main(ActivityThread.java:5696)                                                                                at java.lang.reflect.Method.invoke(Native Method)                                                                                at java.lang.reflect.Method.invoke(Method.java:372)                                                                                at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)                                                                                at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:823)05-01 23:45:47.083 1756-3399/com.donateblood.blooddonation D/Process: killProcess, pID=175605-01 23:45:47.083 1756-3399/com.donateblood.blooddonation D/Process: com.androID.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 
package com.donateblood.blooddonation;import androID.app.ProgressDialog;import androID.content.Intent;import androID.os.AsyncTask;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.Widget.ArrayAdapter;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Spinner;import androID.Widget.Toast;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;import java.util.ArrayList;import java.util.List;import butterknife.ButterKnife;import butterknife.InjectVIEw;/** * Created by YouCaf Iqbal on 4/6/2016. */public class MainGUI extends AppCompatActivity {    public static List<DonorPerson> Donors = new ArrayList<DonorPerson>();    @InjectVIEw(R.ID.findppl) button _findbutton;    GPSTracker gps;    private double latitude;    private double longitude;    DB db; String test="";    DBCursor cursor;    DBCollection collection;    Database dataobj = new Database();    ArrayList allPPLlat = new ArrayList();    ArrayList allPPLlong = new ArrayList();    ArrayList allPPLNumbers = new ArrayList();    ArrayList allPPLnames = new ArrayList();    ArrayList allPPlimages = new ArrayList();    ArrayList allPPLEmails = new ArrayList();    ArrayList SelectedPPLlat = new ArrayList();    ArrayList SelectedPPLlong = new ArrayList();    public Spinner mySpinner;    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.maingui);        ButterKnife.inject(this);        getSupportActionbar().setdisplayHomeAsUpEnabled(true);        Spinner spinner =(Spinner) findVIEwByID(R.ID.spinner);        String[] List = getResources().getStringArray(R.array.blood_type);        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.spinner_layout,R.ID.txt,List);        spinner.setAdapter(adapter);        _findbutton.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                getCurrentLatLong();                dbAsync thrd = new dbAsync();                thrd.execute();            }            //distance=distance(lablat, lablong, curlat, curlong);        });    }    public voID getCurrentLatLong(){        gps = new GPSTracker(MainGUI.this);        if (gps.canGetLocation()) {            latitude = gps.getLatitude();            longitude = gps.getLongitude();        }    }    @OverrIDe    public boolean onoptionsItemSelected(MenuItem item) {        if(item.getItemID()==androID.R.ID.home){            finish();        }        return super.onoptionsItemSelected(item);    }    public class dbAsync extends AsyncTask<VoID,VoID,VoID>{        private ProgressDialog pDialog;        @OverrIDe        protected VoID doInBackground(VoID... voIDs) {            getotherLatLong();            return null;        }        @OverrIDe        protected voID onPreExecute() {            super.onPreExecute();            pDialog = new ProgressDialog(MainGUI.this);            pDialog.setMessage("Searching people nearby...");            pDialog.setIndeterminate(false);            pDialog.setCancelable(true);            pDialog.show();        }        @OverrIDe        protected voID onPostExecute(VoID aVoID) {            super.onPostExecute(aVoID);            pDialog.dismiss();            Intent intent = new Intent(getApplicationContext(), PeopleList.class);            startActivity(intent);           // Toast.makeText(getBaseContext(), "Near by latitudes "+SelectedPPLlat, Toast.LENGTH_LONG).show();           // Toast.makeText(getBaseContext(), "Near by longitudes "+SelectedPPLlong, Toast.LENGTH_LONG).show();        }    }    public voID getotherLatLong() {        db = dataobj.getconnection();        collection = db.getCollection("UserDetails");        cursor = collection.find();        while (cursor.hasNext()) {            DBObject doc = cursor.next();            // Lats longs used in the next for Loop for calculation distances            allPPLlat.add(doc.get("lat"));            allPPLlong.add(doc.get("long"));            // All these other arrayLists are used to store object of a donor person            allPPLEmails.add(doc.get("number").toString());            allPPLnames.add(doc.get("name").toString());            allPPlimages.add(doc.get("image").toString());            allPPLEmails.add(doc.get("email").toString());        }        for(int i =0;i<allPPLlat.size();i++){           double dist= distance((double)allPPLlat.get(i),(double)allPPLlong.get(i),latitude,longitude);            dist=dist/1000;            if(dist<20){                Donors.add(new DonorPerson(""+allPPLnames.get(i)+"", ""+allPPLEmails.get(i)+"" ,""+allPPLNumbers.get(i)+"" ,""+allPPlimages.get(i)+""));            }        }    }    public double distance(double lat1, double lon1, double lat2, double lon2) {        double R = 6371.0; // km        double dLat = (lat2 - lat1) * Math.PI / 180.0;        double dLon = (lon2 - lon1) * Math.PI / 180.0;        lat1 = lat1 * Math.PI / 180.0;        lat2 = lat2 * Math.PI / 180.0;        double a = Math.sin(dLat / 2.0) * Math.sin(dLat / 2.0) +                Math.sin(dLon / 2.0) * Math.sin(dLon / 2.0) * Math.cos(lat1) * Math.cos(lat2);        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));        double d = R * c;        return d * 1000; // return distance in m    }}

解决方法:

在这一部分上:

for(int i =0;i<allPPLlat.size();i++){           double dist= distance((double)allPPLlat.get(i),(double)allPPLlong.get(i),latitude,longitude);            dist=dist/1000;            if(dist<20){                Donors.add(new DonorPerson(""+allPPLnames.get(i)+"", ""+allPPLEmails.get(i)+"" ,""+allPPLNumbers.get(i)+"" ,""+allPPlimages.get(i)+""));            }        }

您正在获取一个空数组的位置0的get,该数组不存在并且抛出您所看到的错误.堆栈跟踪实际上指向您确切的位置.

总结

以上是内存溢出为你收集整理的Java IndexOutOfBound异常在Android中使用arraylist全部内容,希望文章能够帮你解决Java IndexOutOfBound异常在Android中使用arraylist所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1095222.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存