简体   繁体   English

跨度列导致NullPointerException

[英]Span columns causes NullPointerException

I simply want to modify the span layout attribute of a textview (called t) in order to let it span 2 columns inside a table. 我只想修改textview的span布局属性(称为t),以使其跨越表中的2列。 I use the code 我使用代码

TextView t = new TextView(WineActivity.this);
t.setTextSize(15);
TableRow.LayoutParams params = (TableRow.LayoutParams) t.getLayoutParams();
params.span = 2; 
t.setLayoutParams(params);

Quite misteriously to me, the second line of the code generates a NullPointerException. 对我而言,代码非常糟糕,第二行生成了NullPointerException。 Does anybody know why? 有人知道为什么吗?

"params" is null. “参数”为空。

I'm guessing that t.getLayoutParams is probably null as well, but seeing more code would help us. 我猜t.getLayoutParams可能也为null,但是看到更多代码会有所帮助。

The problem is you're calling (TableRow.LayoutParams) t.getLayoutParams(); 问题是您正在调用(TableRow.LayoutParams)t.getLayoutParams(); but t doesn't generate LayoutParams until the program is already running and showing the page. 但在程序已经运行并显示页面之前,t不会生成LayoutParams。

The simplest solution is just to create new params yourself. 最简单的解决方案是自己创建新的参数。

TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 3;
row.setLayoutParams(params);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM