(TableLayout)findViewById(空指针异常((TableLayout)findV

2019-09-20 05:59发布

我要寻找的答案整天,但是当我试图让我有空表格的布局。 我发现,它可能是另一个表的布局,相同名称的信息,但只有一个。 我的.xml文件:

我的XML

我的代码是在这里layout6 = (TableLayout)findViewById(R.id.shdslResultsTableLayout); layout6.setVisibility(0); layout6 = (TableLayout)findViewById(R.id.shdslResultsTableLayout); layout6.setVisibility(0);

总是以空指针结束。 请帮忙。

Answer 1:

如果findViewById()返回一个NPE,尝试下列:

  • 通过清洁工程项目 - >清除... - >检查项目 - >确定
  • 确保你的ID绝对没有拼写错误
  • 确保内容查看正在显示正确的布局,其中TableLayout存在
  • 确保你setContentView之前findViewById

我假设我已经列出的第三个选项是最有可能您的问题。

重新清洗项目的每个步骤后,也可能有帮助。



Answer 2:

请确保您拨打的正确布局setContentView方法,并确保你调用setContentView前调用findViewById

如果你所做的,然后尝试清理/重建。

希望这个能对您有所帮助



文章来源: (TableLayout)findViewById( null pointer exception