好的,所以我创建了一个带有几个布局和文本视图的自定义视图。我制作了一个动态创建视图的对象。这是制作视图并设置 TextViews 的代码......
inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.run_info_layout, null);
TextView infoP, in;
infoP = (TextView) layout.findViewById(R.id.tvInfoParameter);
in = (TextView) layout.findViewById(R.id.tvInfo);
infoP.setText(ip);
in.setText(i);
在一个活动中,我有它使用此代码创建其中一些对象....
runInfo.add(new RunInfoLayout("Final Time", run.getTime(), RunEnding.this));
runInfo.add(new RunInfoLayout("Distance", Float.toString(run.getDistance()), RunEnding.this));
runInfo.add(new RunInfoLayout("Started", run.getBTime(), RunEnding.this));
runInfo.add(new RunInfoLayout("Ended", run.getETime(), RunEnding.this));
runInfo.add(new RunInfoLayout("Number of laps", Integer.toString(numberOfLaps), RunEnding.this));
我有一个循环通过 runInfo 这是一个 ArrayList 并将视图添加到 ScrollView .....
for(int x = 0; x < runInfo.size(); x++){
llRunInfo.addView(runInfo.get(x).getView());
}
llRunInfo 是 ScrollView 中的 LinearLayout。getView() 方法只返回它在早期代码中创建的视图。问题出在 addView() 行。我在那里得到一个 nullPointerException 。这是日志猫....
07-25 12:42:25.223: E/AndroidRuntime(31056): Caused by: java.lang.NullPointerException
07-25 12:42:25.223: E/AndroidRuntime(31056): at android.view.ViewGroup.addView(ViewGroup.java:3148)
07-25 12:42:25.223: E/AndroidRuntime(31056): at android.view.ViewGroup.addView(ViewGroup.java:3131)
07-25 12:42:25.223: E/AndroidRuntime(31056): at com.TBJsoft.runprogress.RunEnding.build(RunEnding.java:55)