java - 在android中用Toast覆盖IndexOutOfBoundsException?

标签 java arraylist indexoutofboundsexception

有一个自动完成 TextView 和按钮,当搜索位置时,它会显示该位置。然后选择位置并按下按钮将获得从我的位置到搜索位置的路线方向。问题是,当我尝试输入诸如“aweafnnanjndj”之类的错误条目并按下按钮时,将收到 IndexoutofBoundsException 并关闭应用程序。任何人都可以帮助修复此错误。是否可以在那里 toast “位置错误”或“重新输入位置”之类的?这是出现错误的代码

    btnEnd= (Button) findViewById(R.id.tab2);
    OnClickListener findClickListener=new OnClickListener() {

        @Override
        public void onClick(View v) {


            checkingNetwork();
            if (isOnline())
            {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(atvPlaces.getWindowToken(), 0);
                atvPlaces.clearFocus();
                String location = atvPlaces.getText().toString();

                if (location != null && !location.equals(""))

                {
                    new GeocoderTask().execute(location);
                }

                Geocoder coder = new Geocoder(getApplicationContext());
                List<Address> address = null;
                try 
                {
                    address = coder.getFromLocationName(location, 1);
                } 
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                if (location != null && !location.equals("") && isOnline()) 
                {
                    locationtest = address.get(0);    <----//Line Number 3354//
                    LatLng fromPosition = new LatLng(
                            locationtest.getLatitude(),
                            locationtest.getLongitude());
                    setMarkerOnLocation(fromPosition);
                    sLat=locationtest.getLatitude();
                    sLong=locationtest.getLongitude();

                    LatLng newPoss=new LatLng(latitude, longitude);
                    setMarkerOnLocation(newPoss);

                }

                else
                {
                    Toast.makeText(getApplicationContext(), "Re enter Location", Toast.LENGTH_SHORT).show();
                }


            }
        }

    };

    btnEnd.setOnClickListener(findClickListener);

这是日志猫

    FATAL EXCEPTION: main
 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
    at java.util.ArrayList.get(ArrayList.java:304)
    at in.wptrafficanalyzer.locationroutedirectionmapv2.MainActivity$7.onClick(MainActivity.java:3354)
    at android.view.View.performClick(View.java:3534)
    at android.view.View$PerformClick.run(View.java:14172)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:4624)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:965)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:732)
    at dalvik.system.NativeStart.main(Native Method)

最佳答案

您应该在第 3354 行之前测试 address 是否为 null 或为空,如果是这种情况,则显示 toast 而不是到达 address.get(0),如下所示:

if (address ==null || address.isEmpty()) {
   Toast.makeText(getApplicationContext(), "wrong address",
        Toast.LENGTH_LONG).show();
} else {
    locationtest = address.get(0);
    ....

关于java - 在android中用Toast覆盖IndexOutOfBoundsException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25377491/

相关文章:

java - ArrayIndexOutOfBounds 但数组大小设置正确

Java:如何有效地从数据库中读取?

java - 字符串索引超出范围错误 7

java - 如何使用 Jackson 反序列化动态字段?

java - 更新值为 List 的 HashMap 条目

java - 如何将 Arraylist<ArrayList<objects>> 转换为 ArrayList<objects>

javascript - JSON 数组值分隔 javascript 中的列表

java - 由于 Apache common.lang ArrayUtils.remove 而出现 IndexOutOfBoundsException

java - Spark on YARN - 任务运行速度非常慢

java - 如何在 recyclerView 中显示不同的布局?