NullPointerException异常在TextView.checkForRelayout(),而的setText() [英] NullPointerException at TextView.checkForRelayout() while setText()
问题描述
我想实现ViewHolder模式,以提高性能比较,而在画布上的地图绘制。
我需要创建一些标签我的标记。这种方法被称为,而在数据处理得出()
I am trying to implement ViewHolder pattern to improve perfomance while drawing on canvas over the map.
I need to create some labels for my markers. This method is called while data processing in draw()
// now we create label view and convert it into bitmap
private Bitmap createLabelBitmap(GisLaborHistoryObject object, HistoryPoint point) {
// create view from xml
if (vh == null) {
vh = new ViewHolder();
vh.view = inflater.inflate(R.layout.map_gis_history_label, null, false);
vh.tv = (TextView) vh.view.findViewById(R.id.gislabeltext);
}
// set label content
if (vh.tv != null) {
if (!mSingleLaborHistory) {
String name = "";
name = object.getDisplayName();
if (name == null || "".equals(name))
name = object.getPersonId();
if (name == null || "".equals(name))
name = object.getLaborCode();
if (name == null)
name = "";
vh.tv.setText(name);
} else
vh.tv.setText(point.getChangeDate());
}
// measure resulting view
vh.view.measure(spec, spec);
vh.view.layout(0, 0, vh.view.getMeasuredWidth(), vh.view.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(vh.view.getWidth(), vh.view.getHeight(), Bitmap.Config.ARGB_8888);
canvasToDraw.setBitmap(b);
canvasToDraw.translate(-vh.view.getScrollX(), -vh.view.getScrollY());
vh.view.draw(canvasToDraw);
return b;
}
规格为
spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
,结果是 logcat的:
the result is Logcat:
04-29 17:12:34.057: E/AndroidRuntime(389): java.lang.NullPointerException
04-29 17:12:34.057: E/AndroidRuntime(389): at android.widget.TextView.checkForRelayout(TextView.java:5497)
04-29 17:12:34.057: E/AndroidRuntime(389): at android.widget.TextView.setText(TextView.java:2730)
04-29 17:12:34.057: E/AndroidRuntime(389): at android.widget.TextView.setText(TextView.java:2598)
04-29 17:12:34.057: E/AndroidRuntime(389): at android.widget.TextView.setText(TextView.java:2573)
04-29 17:12:34.057: E/AndroidRuntime(389): at com.xxx.android.proj.ui.map.overlays.LaborsMarkerOverlay.createLabelBitmap(LaborsMarkerOverlay.java:164)
XML布局我使用
XML layout i use
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gislabeltext"
style="@style/labelStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
有什么可这个错误的原因是什么?
what can be a reason of this error?
推荐答案
在寻找到android的<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3.3_r1/android/widget/TextView.java?av=f">source code 在行5497我注意到,有使用mLayoutParams检查。我看着WHIS情况下,在调试模式,这是真正为空! 我加入其中明确设置此PARAMS行。现在,一切工作正常。
After looking into android source code at line 5497 I noticed that there is a check using mLayoutParams. I looked whis case in the debug mode and it is really was null! I add line which explicitly set this params. Now everything works fine.
vh.tv = (TextView) vh.view.findViewById(R.id.gislabeltext);
vh.tv.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
我的错误是,我必须明确地设置的LayoutParams用的ViewGroup根= NULL充气时; 一切工作正常,直到我尝试重用的TextView乘次。
My error was that i had to set LayoutParams explicitly when inflating with ViewGroup root = null; Everything works fine until I try to reuse TextView multiply times.
这篇关于NullPointerException异常在TextView.checkForRelayout(),而的setText()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!