【发布时间】:2017-03-29 15:47:07
【问题描述】:
如何将上下文应用到 textview 数组?
TextView[] textView = new TextView[3];
for (int cell = 0; cell < 3; cell++){
textView[cell].setText("-");
}
显然,这个错误被抛出:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”
如果这不是 textview array,则在调用构造函数时将活动 context 添加为 parameter 将使对象不为 null,解决我的问题:
for (int cell = 0; cell < 3; cell++){
TextView textView = new TextView(context);
textView.setText("-");
}
在哪里,context = getActivity().getApplicationContext()
如何为 textview 数组设置上下文参数?我想要的是每个文本视图的动态变量名称,因此我可以分别调用各个文本视图。
【问题讨论】:
标签: java android arrays android-activity android-context