ArrayIndexOutOfBoundsException异常在一些特定的Andr​​oid设备 [英] ArrayIndexOutOfBoundsException in some specific Android devices

查看:131
本文介绍了ArrayIndexOutOfBoundsException异常在一些特定的Andr​​oid设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发布在谷歌应用程序播放。在活动中,我填一个表和它的作品在许多不同的设备,包括我的设备MOTOROLA MOTO G.但是,今天我的朋友谁使用LG G3告诉我,它崩溃在某一点时,他向下滚动。所发生的其他一些特定的设备也。

I have just released an application in Google Play. In an activity I am filling a list and it works in many different devices including my device Motorola Moto G. But today my friend who uses LG G3 told me that it crashes at certain point when he scrolls down. That happened some other specific devices also.

当他滚动,并在同一个点列表会出现此错误每次。所以我想这是不是内存错误。我想强调的是,它不能在我的设备发生。下面你可以找到我的code:

This error occurs everytime when he scrolls and at same point of list. So I assume it is not memory error. I want to underline that it does not happen in my device. Below you can find my code:

public View getView(int position, View view, ViewGroup parent) 
{
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.list_programim_item, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.programim_item_name);

    ImageView imageView = (ImageView) rowView
            .findViewById(R.id.programim_item_img);

    // Don't use web but getItem call on the adapter itself
    txtTitle.setText(getItem(position));
    txtTitle.setTypeface(font);
    imageView.setImageResource(imageId[position]); //CRASHES HERE ArrayIndexOutOfBoundsException
    return rowView;
}

我从一个文本文件填充imageId。

I am populating imageId from a text file.

    public void setHareketler() {
    AssetManager am = getAssets();
    InputStreamReader ims = null;
    BufferedReader reader = null;
    String data = "File not available!";
    try {
        ims = new InputStreamReader(am.open(hareketTipi + ".txt"), "UTF-8");
        reader = new BufferedReader(ims);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (ims != null) {
        try {
            String mLine = reader.readLine();
            data = "";
            while (mLine != null) {
                data += mLine;

                // mLine bossa dur
                if (mLine == null)
                    break;

                arrHareketIsimleri.add(mLine); // hareket isimleri eklendi

                // buradan itibaren hareket resimleri eklenmesi icin
                mLine = mLine.replace(" ", "_");
                mLine = mLine.replace("-", "_");
                mLine = mLine.replace("(", "");
                mLine = mLine.replace(")", "");
                mLine = mLine.toLowerCase();
                mLine = "_" + mLine + "_thumb";
                try {
                    Class res = R.drawable.class;
                    //mLine = "_6_inch_leg_lifts"; // bu kaldirilacak
                    Field field = res.getField(mLine);
                    int drawableId = field.getInt(null);
                    arrHareketResimleri.add(drawableId); // hareket
                                                            // resimleri
                                                            // ekleniyor
                } catch (Exception e) {
                    Log.e("MyTag", "Failure to get drawable id.", e);
                }
                mLine = reader.readLine();

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<ImageView
    android:id="@+id/programim_item_img"
    android:layout_height="60dp"
    android:layout_width="60dp"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="4dp"
    android:layout_centerVertical="true"/>

<TextView 
    android:id="@+id/programim_item_name"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/programim_item_img"
    android:layout_toLeftOf="@+id/programim_right_arrow"
    android:layout_marginLeft="16dp"
    android:textSize="26sp"
    android:textColor="@color/dark_gray" 
    android:layout_centerVertical="true"/>

<ImageView
    android:id="@+id/programim_right_arrow"
    android:src="@drawable/right_arrow"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="4dp"
    android:layout_centerVertical="true"/>

和我附上崩溃日志截图。我想知道什么可能导致这个崩溃在不同的设备。 在这里输入的形象描述

and I am attaching screenshots of the crash log. I am wondering what may cause this crash in different devices.

推荐答案

看来,(与给定的信息),当你达到你的ListView的结尾,你面对这个问题。我不认为是设备相关的,但似乎这个错误并不总是触发本身。

It seems that (with the given information) that when you reach the end of your listview, you face this issue. I do not think is device-related but it seems that this error is not always triggering itself.

我的建议:

请确保您的数组 imageId 的长度的大小与你的的ListView 相同。使用 getCount将()方法适配器为。看来你的ListView正在试图获得该项目45号(44位,因为它从0开始)和你的数组长度为44。

Make sure that the length of your array imageId has the same size as your ListView. Use the getCount() method in your adapter for that. It seems your listView is trying to get the item number 45 (position 44, as it starts from 0) and your array length is 44.

如果这是一个错误,有什么不这么认为我和你没有发现任何其他的解决办法,尽量忽略那些行如下:

If it is a bug, what I do not think so and you do not find any other solutions, try to ignore those rows as follows.

if(position < imageId.length)
{
   txtTitle.setText(getItem(position));
   txtTitle.setTypeface(font);
   imageView.setImageResource(imageId[position]);
}

此方式,您将获得空行,但它可以帮助你找到你的问题。

This way you will get empty rows, but it may help you find your problem

希望它能帮助

这篇关于ArrayIndexOutOfBoundsException异常在一些特定的Andr​​oid设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆