【问题标题】:NullPointerException at TextView.checkForRelayout() while setText()在 setText() 时 TextView.checkForRelayout() 出现 NullPointerException
【发布时间】:2013-04-29 14:35:01
【问题描述】:

我正在尝试实现 ViewHolder 模式以提高性能,同时在地图上的画布上绘图。 我需要为我的标记创建一些标签。在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);

结果是 日志猫:

 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 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" />

这个错误的原因是什么?

【问题讨论】:

标签: android android-view


【解决方案1】:

在第 5497 行查看 android source code 后,我注意到使用 mLayoutParams 进行了检查。我在调试模式下查看了这个案例,它真的是空的! 我添加了明确设置此参数的行。现在一切正常。

vh.tv = (TextView) vh.view.findViewById(R.id.gislabeltext);
vh.tv.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

我的错误是在使用 ViewGroup root = null; 进行膨胀时,我必须明确设置 LayoutParams; 在我尝试多次重复使用 TextView 之前,一切正常。

【讨论】:

    【解决方案2】:

    您需要在您的 ui 线程中调用 vh.tv.setText 方法。所以请确保你正在这样做。

    【讨论】:

    • 此示例适用于 Overlay 的 draw(Canvas canvas, MapView mapView, boolean shadow) 方法。
    猜你喜欢
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 2018-05-22
    • 1970-01-01
    相关资源
    最近更新 更多