Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表布局未显示&getChildCount导致异常,android_Android_Android Tablelayout - Fatal编程技术网

表布局未显示&getChildCount导致异常,android

表布局未显示&getChildCount导致异常,android,android,android-tablelayout,Android,Android Tablelayout,我使用一个记录在案的示例来试验表布局。对于一个barebone MainActivity,它在setContentViewR.layout.activity_main之前只有以下代码,emulator显示的表布局很好。然后,我在setContentView行之后添加了这些行,该操作导致: 即使在执行之后,布局也不会显示在仿真器中 setContentView行,当代码在上的断点处停止时 tableLayout声明行 在具有第一个getChildCount的行中出错。 Main活动中的代码如下所示

我使用一个记录在案的示例来试验表布局。对于一个barebone MainActivity,它在setContentViewR.layout.activity_main之前只有以下代码,emulator显示的表布局很好。然后,我在setContentView行之后添加了这些行,该操作导致:

即使在执行之后,布局也不会显示在仿真器中 setContentView行,当代码在上的断点处停止时 tableLayout声明行 在具有第一个getChildCount的行中出错。 Main活动中的代码如下所示: }
我错过了什么?提前感谢您的帮助。

您必须按如下方式初始化您的表格布局:

TableLayout tableLayout = findViewById(R.id.whatever_id_you_gave_to_the_tablelayout);

正如Luksprog所说,tableLayout是空的,那么如何获得一个空值的getChildCount。因此,给它赋值

TableLayout tableLayout = findViewById(R.id.your_table_layout_id);

这里,您的\u table\u layout\u id是您在xml文件中为表布局指定的id。

如果tableLayout引用为null,那么在调用此null引用上的getChildCount时,您预计会发生什么情况?感谢所有响应。我在想我自己。我从另一个博客复制了代码,该博客在调用get child之前将tablelayout设置为null。我认为这是android的一个特别之处,但我一定是误解了什么。我想没有在带有bug的emulator上显示布局是因为setcontentview生成了一个单独的线程,并且在emulator被bug停止之前没有显示布局?
TableLayout tableLayout = findViewById(R.id.your_table_layout_id);