English 中文(简体)
为什么调适器.add () 会产生无限循环和崩溃?
原标题:Why does adapter.add() create an infinite loop and crash?

我制作了安卓应用程序, 并安装了 addter.add () 似乎崩溃了该应用程序。 我不得不使用日志标签来发现我的代码被困在一个循环圈里。 这里的代码是 :

int i =0;
if(cards.size()>0){
    Log.i("KOOL","Checked arraylist size =" + cards.size());
    while(i < cards.size()){
        Log.i("KOOL","Inside while loop");
        adapter.add(cards.get(i));
        i++;
    }
    Log.i("KOOL","Added data to adapter");
    adapter.notifyDataSetChanged();
    Log.i("KOOL","Finished OnActivityResult");
}

程序会到达日志标签“ 循环时在内部” 并重复到程序崩溃 。 我知道在循环前 < code> cards. size () 是 2 。 因此我看不出有什么理由要崩溃该程序 。 我是否正确使用适配器? 请帮助! @ info/ plain

最佳回答

您的问题缺少某些上下文 。 幸运的是, 我知道您的适配器属于 < code> ArrayAdapter 类型, 并且正在以 < code> cards 初始化为 < code > cards > 。 当您开始循环时, < code> cards. size () 是 < strong > 2 < / strong > 。 尺寸() < code > add < / code > 是 < / code > 。 但是, 您却在它上添加了 < code > add < / code > 的东西, 使它成为 < code> cards. size () < strong > 3 < 3 。 然后... I guess you get the point.

这里是 ArrayAdapter 的代码: 添加 :

public void add(T object) {
    synchronized (mLock) {
        if (mOriginalValues != null) {
            mOriginalValues.add(object);
        } else {
            mObjects.add(object);
        }
    }
    if (mNotifyOnChange) notifyDataSetChanged();
}

此处 m ResourchValues 是您用来构建适配器的相同列表。 mObjects 是过滤过的版本。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签