问题描述
我目前具有不能够运行在Android应用开发以下code中的问题。
I am currently having the problem of not able to run the following code in Android app development.
import java.util.ArrayList;
public class Test extends FragmentActivity {
ArrayList<String> random;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
for (int a=0; a<11; a++){
random.add("a");
}
}
}
我知道上面code做无用的动作,但是是从我的问题,简化了Java中环code。而我从错误日志,未处理的事件循环异常这个错误。任何人都可以指出的是什么,我做错了吗?
I know the above code does useless action but that is simplified from my problem in the for loop code in JAVA. And I got this error from the error log, "unhandled event loop exception". Can anyone point out that what I am doing wrong please?
推荐答案
有至少两个问题(我怀疑)。
There are at least two problems (I suspect).
首先,你得到 NullPointerException异常
因为你没有初始化随机
用值指的是一个实际的对象。
First, you're getting a NullPointerException
because you're not initializing random
with a value referring to an actual object.
接下来,你的语法是坏在这里:
Next, your syntax is bad here:
for (int a=0; a<11; a++);
您code仅增加单个元素随机
- 这是等同于:
Your code is only adding a single element to random
- it's equivalent to:
for (int a=0; a<11; a++)
{
}
random.add("a");
我很怀疑这是你打算什么。我的猜测是,你想这个代替:
I very much doubt that that's what you were intending. My guess is that you wanted this instead:
for (int a=0; a<11; a++)
{
random.add("a");
}
这篇关于无法添加元素ArrayList的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!