java.lang.ArrayIndexOutOfBoundsException:length = 10; index = 10错误

时间:2017-01-11 11:53:20

标签: android

我有四个活动使用单个适配器来显示微调器。调用适配器时,其中2个活动正常,但其中2个崩溃。给 ArrayIndexOutOfBoundsException:length = 10; index = 10错误

这是代码

此活动不会崩溃并完美显示微调器及其项目

public class ArcherTowerActivity extends AppCompatActivity {

 Spinner sp1;
    Spinner sp2;
    Spinner sp3;
    Spinner sp4;
    Spinner sp5;
    Spinner sp6;
    Spinner sp7;
    Spinner sp8;
    TextView tx;
    Button sbmt;
    String levels[]={"Level 1",
            "Level 2",
            "Level 3",
            "Level 4",
            "Level 5",
            "Level 6",
            "Level 7",
            "Level 8",
            "Level 9",
            "Level 10",
            "Level 11",
            "Level 12",
            "Level 13",
            "Level 14",};
    int[] images={
            R.drawable.archer_tower1,
            R.drawable.archer_tower2,
            R.drawable.archer_tower3,
            R.drawable.archer_tower4,
            R.drawable.archer_tower5,
            R.drawable.archer_tower6,
            R.drawable.archer_tower7,
            R.drawable.archer_tower8,
            R.drawable.archer_tower9,
            R.drawable.archer_tower10,
            R.drawable.archer_tower11,
            R.drawable.archer_tower12,
            R.drawable.archer_tower13,
            R.drawable.archer_tower14,
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_archer_tower);

        sbmt=(Button)findViewById(R.id.submit);
        tx=(TextView)findViewById(R.id.upgradeResult);
        sp1=(Spinner)findViewById(R.id.spinner);
        sp2=(Spinner)findViewById(R.id.spinner1);
        sp3=(Spinner)findViewById(R.id.spinner2);
        sp4=(Spinner)findViewById(R.id.spinner3);
        sp5=(Spinner)findViewById(R.id.spinner4);
        sp6=(Spinner)findViewById(R.id.spinner5);
        sp7=(Spinner)findViewById(R.id.spinner6);
        sp8=(Spinner)findViewById(R.id.spinner7);
        SpinnerAdapter adapter=new SpinnerAdapter(this,levels,images);
        sp1.setAdapter(adapter);
        sp2.setAdapter(adapter);
        sp3.setAdapter(adapter);
        sp4.setAdapter(adapter);
        sp5.setAdapter(adapter);
        sp6.setAdapter(adapter);
        sp7.setAdapter(adapter);
        sp8.setAdapter(adapter);

此活动无法执行此操作

public class MortarActivity extends AppCompatActivity {

    Spinner sp1;
    Spinner sp2;
    Spinner sp3;
    Spinner sp4;

    TextView tx;
    Button sbmt;
    String levels[]={"Level 1",
            "Level 2",
            "Level 3",
            "Level 4",
            "Level 5",
            "Level 6",
            "Level 7",
            "Level 8",
            "Level 9",
            "Level 10",
           };
    int[] images={
            R.drawable.mortar1,
            R.drawable.mortar2,
            R.drawable.mortar3,
            R.drawable.mortar4,
            R.drawable.mortar5,
            R.drawable.mortar6,
            R.drawable.mortar7,
            R.drawable.mortar8,
            R.drawable.mortar9,
            R.drawable.mortar10,

    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mortar);

        sbmt=(Button)findViewById(R.id.submit);
        tx=(TextView)findViewById(R.id.upgradeResult); 
        sp1=(Spinner)findViewById(R.id.spinner);
        sp2=(Spinner)findViewById(R.id.spinner1);
        sp3=(Spinner)findViewById(R.id.spinner2);
        sp4=(Spinner)findViewById(R.id.spinner3);

        SpinnerAdapter adapter=new SpinnerAdapter(this,levels,images);
        sp1.setAdapter(adapter);
        sp2.setAdapter(adapter);
        sp3.setAdapter(adapter);
        sp4.setAdapter(adapter);

这是适配器代码

public class SpinnerAdapter extends ArrayAdapter <String>{


Context c;

    String[] levels;
    int[] images;

    public SpinnerAdapter(Context ctx,String[] levels, int[] images){

        super(ctx,R.layout.spinner_building_to_upgrade,levels);
        this.c=ctx;
        this.images=images;
        this.levels=levels;


    }


    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {


        if(convertView==null){

            LayoutInflater inflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.spinner_building_to_upgrade,null);
        }

        TextView tx=(TextView) convertView.findViewById(R.id.levelNumber);
        ImageView image=(ImageView)convertView.findViewById(R.id.imageid);

        //SET DATA
        tx.setText(levels[position]);
        image.setImageResource(images[position]);




        return convertView;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){

            LayoutInflater inflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.spinner_building_to_upgrade,null);
        }

        TextView tx=(TextView) convertView.findViewById(R.id.levelNumber);
        ImageView image=(ImageView)convertView.findViewById(R.id.imageid);

        //SET DATA
        tx.setText(levels[position]);
        image.setImageResource(images[position]);



        return convertView;
    }
}

First Activity有14个项目,Second Activity有10个项目。第二个崩溃给出了java.lang.ArrayIndexOutOfBoundsException:length = 10; index = 10错误。

编辑:

我已经找到了问题,如果我在阵列适配器中输入小于10的项目,它会崩溃并给出错误。为什么会这样?因为我没有设置任何最大最小长度。我想在微调器适配器中发送8个项目。请帮忙

错误记录

01-11 17:44:51.066 19769-19769/com.curtztech.android.clashmate E/dalvikvm: Could not find class 'android.widget.ThemedSpinnerAdapter', referenced from method android.support.v7.widget.AppCompatSpinner$DropDownAdapter.<init>
01-11 17:44:51.206 19769-19769/com.curtztech.android.clashmate E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.curtztech.android.clashmate, PID: 19769
java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
at com.curtztech.android.clashmate.SpinnerAdapter.getView(SpinnerAdapter.java:74)
at android.widget.Spinner.makeView(Spinner.java:592)
at android.widget.Spinner.layout(Spinner.java:540)
at android.widget.Spinner.onLayout(Spinner.java:502)
at android.view.View.layout(View.java:14834)
at android.view.ViewGroup.layout(ViewGroup.java:4635)
at android.widget.GridLayout.onLayout(GridLayout.java:1183)
at android.view.View.layout(View.java:14834)
at android.view.ViewGroup.layout(ViewGroup.java:4635)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14834)
at android.view.ViewGroup.layout(ViewGroup.java:4635)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14834)
at android.view.ViewGroup.layout(ViewGroup.java:4635)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
at android.view.View.layout(View.java:14834)
at android.view.ViewGroup.layout(ViewGroup.java:4635)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14834)
at android.view.ViewGroup.layout(ViewGroup.java:4635)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14834)
at android.view.ViewGroup.layout(ViewGroup.java:4635)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14834)
at android.view.ViewGroup.layout(ViewGroup.java:4635)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5095)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
01-11 17:45:00.036 20127-20127/? E/Zygote: Zygote:  error closing descriptor
libcore.io.ErrnoException: close failed: EBADF (Bad file number)
at libcore.io.Posix.close(Native Method)
at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
at com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:198)
at com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879)
at com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:242)
at com.android.internal.os.ZygoteInit.runSelectLoop(ZygoteInit.java:662)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
at dalvik.system.NativeStart.main(Native Method)
01-11 17:45:01.006 1230-2336/? E/ActivityThread: Failed to find provider info for com.android.settings.applications.SpProvider
01-11 17:45:01.006 20160-20160/? E/Zygote: Zygote:  error closing descriptor
libcore.io.ErrnoException: close failed: EBADF (Bad file number)
at libcore.io.Posix.close(Native Method)
at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
at com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:198)
at com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879)
at com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:242)
at com.android.internal.os.ZygoteInit.runSelectLoop(ZygoteInit.java:662)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
at dalvik.system.NativeStart.main(Native Method)
01-11 17:45:57.236 1230-2340/? E/ActivityThread: Failed to find provider info for com.android.settings.applications.SpProvider
01-11 17:45:57.286 1230-1240/? E/ActivityThread: Failed to find provider info for com.android.settings.applications.SpProvider
01-11 17:46:37.266 1230-1742/? E/LocSvc_ApiV02: E/virtual loc_api_adapter_err LocApiV02::setXtraData(char*, int):1117]: failed status = eLOC_CLIENT_SUCCESS, inject_pos_ind.status = eQMI_LOC_GENERAL_FAILURE_V02, part num = 25, ind.partNum = 0

1 个答案:

答案 0 :(得分:1)

实施此方法;

@Override
public int getCount() {
    return levels.length;
}