关于android:TextView与自定义样式导致奇怪的膨胀异常

TextView with custom style causing strange inflate exception

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

下面是我的布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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样式

1
2
3
4
5
<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以显示在我的列表视图上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@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;
        }
    }

我得到的错误是:

Process: com.BoardiesITSolutions.CritiMonApp, PID: 1451
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null
object reference
at android.widget.AbsListView.obtainView(AbsListView.java:2360)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1673)
at android.widget.AbsListView.onLayout(AbsListView.java:2148)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.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.java:1466)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.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.java:1466)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.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)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(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)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.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)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15596)
at android.view.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很棘手。

确保你有价值。 你可以使用像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 @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必须是维度值所以改变你的风格,

1
2
3
4
<style name="CrashGroup" parent="@android:style/TextAppearance.Small">
        <item name="android:singleLine">false</item>
        <item name="android:gravity">center_vertical</item>
    </style>