TextView with custom style causing strange inflate exception

I am working on an Android project and I am creating a custom list view. The list view contains multiple text views, and I am creating a style for my text view so I haven't got to duplicate the same style over and over again.

Below is my layout

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

Below is my style for my 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>

Below is how I am inflating my XML to be displayed on my list view.

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

The error I am getting is:

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.se tChildFrame(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.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) 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(Linear Layout.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)

It seems to be something to do with the style as if I remove this style attribute from the text views it seems to work OK.

Thanks for any help you can provide.


I think the convertView you are returning is null at times, because handling the convertView is tricky.

Make sure you have a value. You can use smth like :

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

Hope it helps.


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/16634.html

上一篇: 排除NullPointerException

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