Java Android Studio:视图迭代返回空值

Java Android Studio:视图迭代返回空值,java,android,Java,Android,我在尝试将视图放入带有循环的arrayList时遇到了一个问题 这项工作: String textV = "chkBox1Text"; int textI = getResources().getIdentifier(textV, "id", getPackageName()); TextView test = (TextView)findViewById(textI); test.setText("Test 01"); 然而这 ArrayList<TextView> friend

我在尝试将视图放入带有循环的arrayList时遇到了一个问题

这项工作:

String textV = "chkBox1Text";
int textI = getResources().getIdentifier(textV, "id", getPackageName());
TextView test = (TextView)findViewById(textI);
test.setText("Test 01");
然而这

ArrayList<TextView> friendNames = new ArrayList<TextView>();
for(int i = 0; i < 10; i++){
        String textViewID = "chkBox" + i+1 + "Text";
        int current = getResources().getIdentifier(textViewID, "id", getPackageName());
        TextView currentTV = (TextView)findViewById(current);
        friendNames.add(currentTV);
    }
friendNames.get(0).setText("Test 01");  // 0 or 1 
ArrayList friendNames=new ArrayList();
对于(int i=0;i<10;i++){
字符串textViewID=“chkBox”+i+1+“Text”;
int current=getResources();
TextView currentTV=(TextView)findViewById(当前);
friendNames.add(当前电视);
}
friendNames.get(0.setText(“Test 01”);//0或1
返回空指针异常:

原因:java.lang.NullPointerException:尝试调用虚拟机 方法'void android.widget.TextView.setText(java.lang.CharSequence)' 关于空对象引用 在 ******.FriendActivity.onCreate(FriendActivity.java:56)


我做错什么了吗

很难确认,但我认为你的问题可能是:

friendNames.add((TextView)findViewById(currentTV));
应该是:

friendNames.add(currentTV);
因为
currentTV
已经是一个文本视图,您可以在前一行中找到它。

新手错误

 String textViewID = "chkBox" + i+1 + "Text";
应该是

 String textViewID = "chkBox" + (i+1) + "Text";

多亏了迈克

啊,对不起,已经修好了。这只是我在代码中的一个输入错误。这是你怎么说的。谢谢您应该检查循环中
textViewID
的值。你会明白问题出在哪里我就知道那是件愚蠢的事。。。真不敢相信我错过了。谢谢你,迈克。它并不是只添加i+1连接。