当前位置: 首页 > 知识库问答 >
问题:

具有自定义样式的TextView导致奇怪的膨胀异常

澹台展鹏
2023-03-14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/txtFirstOccurred"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="@style/CrashGroup"/>

    <TextView android:id="@+id/txtLastOccurred"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="@style/CrashGroup"/>
</LinearLayout>
<style name="CrashGroup">
        <item name="android:singleLine">false</item>
        <item name="android:textSize">@android:style/TextAppearance.Small</item>
        <item name="android:gravity">center_vertical</item>
    </style>
@Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        try
        {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.android_crash_group_layout, null);

            TextView txtFirstOccurred = (TextView)convertView.findViewById(R.id.txtFirstOccurred);
            txtFirstOccurred.setText(getItem(position).getFirstOccurred());

            TextView txtLastOccurred = (TextView)convertView.findViewById(R.id.txtLastOccurred);
            txtLastOccurred.setText(getItem(position).getLastOccurred());


            return convertView;
        }
        catch (Exception ex)
        {
            Log.e(TAG, ex.toString());
            return null;
        }
    }

我得到的错误是:

它似乎与样式有关,就像我从文本视图中删除这个样式属性一样,它似乎可以正常工作。

共有1个答案

云韬
2023-03-14

我认为您返回的convertView有时是null,因为处理convertView很棘手。

确保你有一个值。您可以使用smth如下:

 @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView text = null;

    if (convertView == null) {
      convertView = inflater.inflate(R.layout.listrow_details, null);
    }

    text = (TextView) convertView.findViewById(R.id.textView1);
    text.setText(children);

    return convertView;
  }

希望有帮助。

 类似资料:
  • 我有一个类似Android Studios的应用程序导航抽屉活动: 我的活动使用Android架构导航组件 到目前为止,一切正常。返回时问题开始: 让我们想象我从“家”——片段甲——导航 Android Studio示例正确运行:C- 我的实现没有:它只是弹出后台C- 我该怎么弥补? 缩小主布局: 导航图: 与我的主要活动onCreate相关的所有导航:

  • 因此,我的日常用户中约有1%经常遇到NavigationView异常,这是由于找不到图像资源造成的。我觉得很奇怪,99%的其他用户都很好。我希望在版本迭代中更新依赖项可以解决这个问题,但不幸的是,这个错误已经在我的Crashlytics日志中保留了6次迭代,这是影响我的用户的最严重的崩溃。 显而易见的是,它只适用于较旧的设备,但这是一个错误的假设。它不是特定于设备的,也不是特定于版本的。尽管可以指

  • 那么如何对此?要扩展什么才能只调用setContentView()或setLayoutResource()? 我看了很多这样的答案,但没有一个符合我的问题。

  • 我有一个片段,我在其中膨胀“Fragment\u board.xml”: 如您所见,fragment\u board包含一个自定义视图“BoardView”,我想从中加载以下“view\u board.xml”: 我的自定义视图包含两个滚动视图(我使用它进行平移),我希望能够在其他布局中重复使用它。BoardView扩展了外部(垂直)滚动视图,如下所示: 当我独立使用它时,它会膨胀得很好,我可以在

  • 我在一本编码书中看到了以下宏定义。 没有任何解释。 请向我解释这些将如何作为和工作。

  • 问题内容: 基本上,该网站可以正常运行12多个小时,然后突然停止工作。我将开始在以前运行良好的LINQ查询中引发奇怪的异常。 我在这篇文章的底部提供了堆栈跟踪。 根据在类似SO帖子上找到的建议,我通过直接从Server Explorer中的DB拖动表来删除并重新制作了DBML。比较Git中的新旧内容,我发现了一些不同的字段: 在dbml中将varchar(255)的一个实例设置为nchar(10)