Java 访问约束/滚动/网格中的数据
我想创建一个新的应用程序。在这个应用程序中,我有一个带有滚动视图的约束布局。在滚动视图中还有一个GridLayout。我现在的问题是如何访问GridLayout中的项 我试了一下:Java 访问约束/滚动/网格中的数据,java,android,Java,Android,我想创建一个新的应用程序。在这个应用程序中,我有一个带有滚动视图的约束布局。在滚动视图中还有一个GridLayout。我现在的问题是如何访问GridLayout中的项 我试了一下: gridLayout = (GridLayout) findViewById(R.id.gridLayout); gridLayout.getColumnCount(); int count = gridLayout.getChildCount(); for(int i = 0 ;
gridLayout = (GridLayout) findViewById(R.id.gridLayout);
gridLayout.getColumnCount();
int count = gridLayout.getChildCount();
for(int i = 0 ; i <count ; i++)
{
TextView child = (TextView) gridLayout.getChildAt(i);
System.out.println(child.getText());
}
错误都是一样的。它是java.lang.NullPointerException。我不明白在ConstraintLayout或ScrollView中不可能从项目中获取访问权限吗?哪一行导致NPE-导致NPE的行将有一个
null
对象。可能findViewById方法找不到视图??我使用视图访问变量的行。所以gridLayout.getColumnCount();constraintLayout.getMaxHeight();和scroll.getMaxScrollAmount();是的,我认为他们找不到视图,但为什么会这样?所以gridlayout是空的if(gridLayout==null){System.err.println(“该死,找不到gridLayout!”);}else{gridLayout.getColumnCount();/…}
。调试无法获取gridLayout的原因。R.id.gridLayout
的值是多少?很抱歉,我来晚了,但我现在已经测试过了。结果如下:if表示找不到GridLayout,而ID为2131558540的Sysout表示:2131558540。我使用gridLayout=(gridLayout)findviewbyd(R.id.gridLayout)初始化变量;为什么它不起作用?奇怪的是,我从每个变量得到错误,而不仅仅是GridLayout。因此,我无法从constraintLayout中访问任何内容。有人能帮我吗?
ConstraintLayout constraintLayout = (ConstraintLayout)findViewById(R.id.fragment_main);
constraintLayout.getMaxHeight();
ScrollView scroll = (ScrollView) findViewById(R.id.scrollView);
scroll.getMaxScrollAmount();