本文介绍了java.lang.ArrayIndexOutOfBoundsException:长度= 1;索引= 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很茫然为什么我收到一个 ArrayIndexOutOfBoundsException异常。请帮我追查。

下面的错误:

 了java.lang.RuntimeException:无法启动活动
   ComponentInfo {es.team.wifi / es.team.wifi.WifiList}:
java.lang.ArrayIndexOutOfBoundsException:长度= 1;索引= 1
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
在android.app.ActivityThread.access $ 700(ActivityThread.java:134)
 

下面是我的活动:

公共类WifiList扩展活动实现OnItemClickListener {    私人元素[]网;    私有静态WifiManager manWifi;    私有静态列表< ScanResult> wifiList;    @燮pressWarnings({静态接入,德precation})    公共无效的onCreate(包savedInstanceState){        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        getWindow()。setFlags(            WindowManager.LayoutParams.FLAG_FULLSCREEN,            WindowManager.LayoutParams.FLAG_FULLSCREEN);        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);        this.manWifi =(WifiManager)getSystemService(Context.WIFI_SERVICE);        如果(this.manWifi.isWifiEnabled()==真){            Toast.makeText(这一点,Capturando REDES ......,Toast.LENGTH_LONG).show();            this.wifiList = this.manWifi.getScanResults();            this.nets =新的元素[wifiList.size()];            的for(int i = 0; I< wifiList.size();我++){                  字符串项= wifiList.get(I)的ToString();                  串[] vector_item = item.split(,);                  串item_essid = vector_item [0];                  串item_capabilities = vector_item [2];                  串item_level = vector_item [3];                  串SSID = item_essid.split(:)[1];                  字符串安全= item_capabilities.split(:)[1];                  字符串功率= item_level.split(:)[1];                  网[我] =新的元素(SSID,安全,电力);            }            的setContentView(R.layout.wifilist);            AdapterElements适配器=新AdapterElements(本);            ListView的网表=(ListView控件)findViewById(R.id.listView1);            netlist.setAdapter(适配器);            netlist.setOnItemClickListener(本);        }        其他 {            AlertDialog alertDialog =新AlertDialog.Builder(本).create();            alertDialog.setTitle(WIFI);            alertDialog.setMessage(ACTIVAR WIFI?\ NSE NECESITA第CONTINUAR。+                \ñ\ñPOR青睐!\ nPulsa ESCANEAR despues日ACTIVAR。 +                \ñ\ñTIEMPO DE ACT:3 Segundos。);            alertDialog.setButton(ACTIVAR,新DialogInterface.OnClickListener(){               公共无效的onClick(最终DialogInterface对话,最终诠释它){                    WifiList.manWifi.setWifiEnabled(真正的);                    Toast.makeText(WifiList.this.getApplicationContext(),                        ACTIVANDO无线..,0).show();                    完();                }            });            alertDialog.setButton2(CANCELAR,新DialogInterface.OnClickListener(){                公共无效的onClick(最终DialogInterface对话,最终诠释它){                    Toast.makeText(WifiList.this.getApplicationContext(),                        CANCELADO WIFI,0).show();                    完();                }            });            alertDialog.setIcon(R.drawable.alert);            alertDialog.show();            //完();        }    }    类AdapterElements扩展ArrayAdapter<对象> {        活动范围内;        公共AdapterElements(活动范围内){            超(背景下,R.layout.elements,网络);            this.context =背景;        }        公共查看getView(INT位置,查看convertView,ViewGroup中父){            LayoutInflater充气= context.getLayoutInflater();            查看项目= inflater.inflate(R.layout.elements,NULL);            TextView的lblTitle =(TextView中)item.findViewById(R.id.str_ssid);            lblTitle.setText(网[位置] .getSSID());            TextView的lblSubtitle =(TextView中)item.findViewById(R.id.str_security);            lblSubtitle.setText(网[位置] .getSecurity());            ImageView的imgPower =(ImageView的)item.findViewById(R.id.img_signal);            字符串cad_pwr =网[位置] .getPower()分裂()[1]的ToString()。            整数PWR =的Integer.parseInt(cad_pwr);            如果(PWR> = -70){                imgPower.setImageResource(R.drawable.wsignal4);            }            否则如果((PWR&所述; -71)及及(PWR> = -80)){                imgPower.setImageResource(R.drawable.wsignal3);            }            否则如果((PWR&所述; 81)及及(PWR> = 85)){                imgPower.setImageResource(R.drawable.wsignal2);            }            其他 {                imgPower.setImageResource(R.drawable.wsignal1);            }            归还物品);        }    }    公共无效onItemClick(适配器视图<> AV,视图V,INT位置,长ARG3){        @燮pressWarnings(静态访问)        字符串项= this.wifiList.get(位置)的ToString();        串[] vector_item = item.split(,);        串item_ssid = vector_item [0];        串item_bssid = vector_item [1];        串item_capabilities = vector_item [2];        串item_level = vector_item [3];        串item_frecuency = vector_item [4];        串SSID = item_ssid.split(:)[1] .substring(1);        串BSSID = item_bssid.substring(7).split()[1];        串sec_aux = item_capabilities.split(:)[1];        字符串安全= NULL;        如果(sec_aux.equals()){            安全=无安全性;        } 其他 {            安全= item_capabilities.split(:)[1] .split(])[0] .substring(2);            如果(security.contains( - )){                System.out.println("&&&&&&&&&&&&&&&&&&&&&& Contiene - );                串P0 = security.split( - )[0];                串P1 = security.split( - )[1];                安全= P0 ++ P1;            }        }        串级= item_level.split(:)[1] .split()[1];        串frecuency = item_frecuency.split(:)[1] .split()[1];        // Creamos意图努埃瓦        意图wifiinfo =新的意图(这一点,WifiInfo.class);        wifiinfo.putExtra(SSID,SSID);        wifiinfo.putExtra(BSSID,BSSID);        wifiinfo.putExtra(安全,安全性);        wifiinfo.putExtra(权力,水平);        wifiinfo.putExtra(frecuency,frecuency);        startActivity(wifiinfo);    }}

解决方案

它的困难没有你的code,但长度为1的阵列,索引1是无效的。试着指数0。

I am at a loss for why I'm receiving an ArrayIndexOutOfBoundsException. Please help me track it down.

Here's the error:

java.lang.RuntimeException: Unable to start activity
   ComponentInfo{es.team.wifi/es.team.wifi.WifiList}:
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.access$700(ActivityThread.java:134)

Here's my activity:

public class WifiList extends Activity implements OnItemClickListener {
    private Element[] nets;
    private static WifiManager manWifi;
    private static List<ScanResult> wifiList;

    @SuppressWarnings({ "static-access", "deprecation" })
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        this.manWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        if (this.manWifi.isWifiEnabled()==true) {
            Toast.makeText(this, "Capturando REDES...", Toast.LENGTH_LONG).show();
            this.wifiList = this.manWifi.getScanResults();

            this.nets = new Element[wifiList.size()];
            for(int i = 0; i < wifiList.size(); i++){
                  String item = wifiList.get(i).toString();
                  String[] vector_item = item.split(",");
                  String item_essid = vector_item[0];
                  String item_capabilities = vector_item[2];
                  String item_level = vector_item[3];
                  String ssid = item_essid.split(":")[1];
                  String security = item_capabilities.split(":")[1];
                  String power = item_level.split(":")[1];
                  nets[i]=new Element(ssid, security, power);
            }


            setContentView(R.layout.wifilist);
            AdapterElements adapter = new AdapterElements(this);
            ListView netlist = (ListView)findViewById(R.id.listView1);
            netlist.setAdapter(adapter);
            netlist.setOnItemClickListener(this);
        }
        else {

            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle(" WIFI ");
            alertDialog.setMessage("   ACTIVAR WIFI ? \nSE NECESITA para CONTINUAR." +
                "\n  \n      POR FAVOR !\nPulsa ESCANEAR despues de ACTIVAR." +
                "\n  \n TIEMPO DE ACT. : 3 Segundos.");
            alertDialog.setButton("ACTIVAR", new DialogInterface.OnClickListener() {
               public void onClick(final DialogInterface dialog, final int which) {
                    WifiList.manWifi.setWifiEnabled(true);
                    Toast.makeText(WifiList.this.getApplicationContext(),
                        "ACTIVANDO WiFi..",0).show();
                    finish();
                }
            });
            alertDialog.setButton2("CANCELAR", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int which) {
                    Toast.makeText(WifiList.this.getApplicationContext(),
                        "CANCELADO WIFI",0).show();
                    finish();
                }
            });
            alertDialog.setIcon(R.drawable.alert);
            alertDialog.show();
            //finish();
        }
    }

    class AdapterElements extends ArrayAdapter<Object> {

        Activity context;

        public AdapterElements(Activity context) {
            super(context, R.layout.elements, nets);
            this.context = context;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = context.getLayoutInflater();
            View item = inflater.inflate(R.layout.elements, null);
            TextView lblTitle = (TextView)item.findViewById(R.id.str_ssid);
            lblTitle.setText(nets[position].getSSID());
            TextView lblSubtitle = (TextView)item.findViewById(R.id.str_security);
            lblSubtitle.setText(nets[position].getSecurity());
            ImageView imgPower = (ImageView)item.findViewById(R.id.img_signal);
            String cad_pwr = nets[position].getPower().split(" ")[1].toString();
            Integer pwr = Integer.parseInt(cad_pwr);

            if (pwr >= -70){
                imgPower.setImageResource(R.drawable.wsignal4);
            }
            else if ((pwr < -71) && (pwr >= -80)){
                imgPower.setImageResource(R.drawable.wsignal3);
            }
            else if ((pwr < 81) && (pwr >= 85)){
                imgPower.setImageResource(R.drawable.wsignal2);
            }
            else {
                imgPower.setImageResource(R.drawable.wsignal1);
            }
            return(item);
        }
    }


    public void onItemClick(AdapterView<?> av, View v, int position, long arg3) {

        @SuppressWarnings("static-access")
        String item = this.wifiList.get(position).toString();
        String[] vector_item = item.split(",");
        String item_ssid = vector_item[0];
        String item_bssid = vector_item[1];
        String item_capabilities = vector_item[2];
        String item_level = vector_item[3];
        String item_frecuency = vector_item[4];
        String ssid = item_ssid.split(":")[1].substring(1);
        String bssid = item_bssid.substring(7).split(" ")[1];
        String sec_aux = item_capabilities.split(":")[1];
        String security=null;
        if (sec_aux.equals(" ")) {
            security="No Security";
        } else {
            security = item_capabilities.split(":")[1].split("]")[0].substring(2);
            if (security.contains("-")){
                System.out.println("&&&&&&&&&&&&&&&&&&&&&& Contiene -");
                String p0 = security.split("-")[0];
                String p1 = security.split("-")[1];
                security = p0 + " " + p1;
            }
        }
        String level = item_level.split(":")[1].split(" ")[1];
        String frecuency = item_frecuency.split(":")[1].split(" ")[1];
        //Creamos Intent nueva
        Intent wifiinfo = new Intent(this, WifiInfo.class);
        wifiinfo.putExtra("ssid", ssid);
        wifiinfo.putExtra("bssid", bssid);
        wifiinfo.putExtra("security", security);
        wifiinfo.putExtra("power", level);
        wifiinfo.putExtra("frecuency", frecuency);
        startActivity(wifiinfo);
    }
}
解决方案

Its difficult without your code, but for array of length 1, index 1 is not valid. Try index 0.

这篇关于java.lang.ArrayIndexOutOfBoundsException:长度= 1;索引= 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 15:55