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

时间:2015-01-02 20:36:10

标签: android listview android-listview layout-inflater inflate-exception

我正在开发一个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:尝试调用虚方法&#39; int android.view.View.getImportantForAccessibility()&#39;在null   对象参考               在android.widget.AbsListView.obtainView(AbsListView.java:2360)               在android.widget.ListView.makeAndAddView(ListView.java:1864)               在android.widget.ListView.fillDown(ListView.java:698)               在android.widget.ListView.fillFromTop(ListView.java:759)               在android.widget.ListView.layoutChildren(ListView.java:1673)               在android.widget.AbsListView.onLayout(AbsListView.java:2148)               在android.view.View.layout(View.java:15596)               在android.view.ViewGroup.layout(ViewGroup.java:4966)               在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.layout(ViewGroup.java:4966)               在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.layout(ViewGroup.java:4966)               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)               在android.widget.FrameLayout.onLayout(FrameLayout.java:508)               在android.view.View.layout(View.java:15596)               在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:15596)               在android.view.ViewGroup.layout(ViewGroup.java:4966)               在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.layout(ViewGroup.java:4966)               在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:890)               在android.view.View.layout(View.java:15596)               在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:15596)               在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:15596)               在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:15596)               在android.view.ViewGroup.layout(ViewGroup.java:4966)               在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.layout(ViewGroup.java:4966)               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)               在android.widget.FrameLayout.onLayout(FrameLayout.java:508)               在android.view.View.layout(View.java:15596)               在android.view.ViewGroup.layout(ViewGroup.java:4966)               在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)               在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)               在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)               在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5779)               在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)               在android.view.Choreographer.doCallbacks(Choreographer.java:580)               在android.view.Choreographer.doFrame(Choreographer.java:550)               在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)

这似乎与样式有关,好像我从文本视图中删除了这个样式属性似乎工作正常。

2 个答案:

答案 0 :(得分:3)

我认为你返回的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;
  }

希望它有所帮助。

答案 1 :(得分:0)

android:textSize必须是尺寸值所以改变你的风格,

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