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

我正在开发一个Android项目,并且正在创建一个自定义列表视图。 列表视图包含多个文本视图,并且为我的文本视图创建一个样式,因此我不必一遍又一遍地重复相同的样式。

以下是我的布局

<?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>

以下是我对textview的样式

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

下面是我如何充气我的XML以显示在我的列表视图中。

@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;
        }
    }

我得到的错误是:

进程:com.BoardiesITSolutions.CritiMonApp,PID:1451 java.lang.NullPointerException:尝试在android.widget.AbsListView.obtainView(AbsListView)的空对象引用上调用虚拟方法'int android.view.View.getImportantForAccessibility()'。在android.widget.ListView.fillFromTop(ListView.java:759)at android.widget.ListView.fillDown(ListView.java:698)在android.widget.ListView.makeAndAddView(ListView.java:1864) android.view.View.layout(View.java:15596)上android.widget.AbsListView.onLayout(AbsListView.java:2148)android.widget.ListView.layoutChildren(ListView.java:1673) .layout(ViewGroup.java:4966)at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)at android.widget.LinearLayout.onLayout(LinearLayout。 android.view.java:15596)android.view.ViewGroup.layout(ViewGroup.java:4966)在android.widget.LinearLayout.se android.widget.Lineout.layoutVertical(LinearLayout.java:1557)在android.widget.LinearLayout.onLayout(LinearLayout.java:1466)android.view.View.layout(View.java)上的tChildFrame(LinearLayout.java:1703) :15596)在android.widget.FrameLayout.onLayout(FrameLayout.java:508)的android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)android.view.ViewGroup.layout(ViewGroup.java:4966)在android .view.java:15596)在android.widget.FrameLayout的android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)android.view.ViewGroup.layout(ViewGroup.java:4966).view.java:15596。 onLayout(FrameLayout.java:508)at android.view.View.layout(View.java:15596)at android.view.ViewGroup.layout(ViewGroup.java:4966)at android.widget.LinearLayout.setChildFrame(LinearLayout.java) :1703)在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)在android.widget.LinearLayout.onLayout(LinearLayout.java:1466)在android.view.View.layout(View.java:15596)在android .view.ViewGroup.lay out(ViewGroup.java:4966)at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:890)at android.view.View.layout(View.java:15596)at android.view.ViewGroup.layout (ViewGroup.java:4966)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)在android.widget.FrameLayout.onLayout(FrameLayout.java:508)在android.view.View.layout(View.java: (android.widget.FrameLayout.onLayout(FrameLayout.java:508))上的android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)上的android.view.ViewGroup.layout(ViewGroup.java:4966)。 android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)android.widget.FrameLayout.onLayout上的android.view.ViewGroup.layout(ViewGroup.java:4966)处的view.View.layout(View.java:15596) (FrameLayout.java:508)在android.view.View.layout(View.java:15596)在android.view.ViewGroup.layout(ViewGroup.java:4966)在android.widget.LinearLayout.setChildFrame(LinearLayout.java: 1703)at android.widget.LinearLayout.layoutVertical(Linear (android.view.java:15596)android.view.ViewGroup.layout(ViewGroup.java:4966)android.widget.LinearLayout.onLayout(LinearLayout.java:1466)在Layout.java:1557) )在android.view.View.layout(View.java:15596)在android.view.android.widget.FrameLayout.onLayout(FrameLayout.java:508)android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) .ViewGroup.layout(ViewGroup.java:4966)at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)at android.view.ViewRootImpl.doTraversal( ViewRootImpl.java:1054)at android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5779)at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)at android.view.Choreographer.doCallbacks(Choreographer) .java:580)at android.view.Choreographer.doFrame(Choreographer.java:550)at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)

这似乎是与样式有关,如果我从文本视图中删除此样式属性,它似乎工作正常。

感谢您的任何帮助,您可以提供。


我认为你所返回的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:textSize必须是维度值所以,像这样改变你的风格,

<style name="CrashGroup" parent="@android:style/TextAppearance.Small">
        <item name="android:singleLine">false</item>
        <item name="android:gravity">center_vertical</item>
    </style>
链接地址: http://www.djcxy.com/p/16633.html

上一篇: TextView with custom style causing strange inflate exception

下一篇: showing full logcat