在Android中用Toast覆盖IndexOutOfBoundsException?

时间:2014-08-19 07:08:55

标签: java arraylist indexoutofboundsexception

自动填充文本视图和按钮,在搜索位置时会显示位置。然后选择位置并按下按钮将获得从我的位置到搜索位置的路线方向。问题是,当我尝试输入错误的条目(如'aweafnnanjndj')并按下按钮时,将获得IndexoutofBoundsException并关闭应用程序。任何人都可以帮助解决此错误。是否有可能在那里'Wrong locaton'或'重新进入该位置'那样举杯祝酒?这是代码获取错误

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

这是logcat

    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)

1 个答案:

答案 0 :(得分:0)

如果address为空或空,您应该在第3354行之前进行测试,如果是这种情况则显示吐司而不是address.get(0),例如:

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