【问题标题】:Override IndexOutOfBoundsException with a Toast in android?在android中用Toast覆盖IndexOutOfBoundsException?
【发布时间】:2014-08-19 07:08:54
【问题描述】:

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

    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)

【问题讨论】:

  • 'address' 存储什么?

标签: java arraylist indexoutofboundsexception


【解决方案1】:

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

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

【讨论】:

  • 非常感谢您的解决方案。错误已修复。我还有一个问题,从 4.1.2 到 4.4 的更高版本的 google mapv2 是否有任何支持权限?有时会自动关闭我创建的谷歌地图应用程序..
猜你喜欢
  • 1970-01-01
  • 2023-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-15
  • 2014-06-12
  • 2015-11-04
  • 2012-11-04
相关资源
最近更新 更多