具有自定义样式的TextView导致奇怪的膨胀异常
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了具有自定义样式的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()'。 java:2360)在android.widget.List上的android.widget.List.fillDown(ListView.java:698)上的android.widget.ListView.makeAndAddView(ListView.java:1864)处于android.widget.ListView.fillFromTop(ListView.java:759) android.widget.List.layoutChildren(ListView.java:1673)位于android.view.View.View的android.view.ViewL上的android.widget.AbsListView.onLayout(AbsListView.java:2148),android.view.layout(View.java:15596) .layout(ViewGroup.java:4966)位于android.widget.LinearLayout.ayLayout(LinearLayout.java:1707),位于android.widget.LinearLayout.onLayout(LinearLayout.jatout)上的android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)。 java:1466)在Android.widget.LinearLayout.se上的android.view.View.layout(View.java:15596)android.view.View。位于android.view.Loutout.Loutout(DirectLayout.java:1466)的android.view.Loutout.Layout.Layout.Layout.Loutout.Tout :15596)在android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)的android.view.FreeLayout.onLayout(FrameLayout.java:508)的android.view.ViewGroup.layout(ViewGroup.java:4966)位于android.widget.FoutLayout的android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)的android.view.ViewGroup.layout(ViewGroup.java:4966)的.view.View.layout(View.java:15596)。 onLayout(FrameLayout.java:508)位于android.view.Viewout.Loutout.setChildFrame(LinearLayout.java :1703)在android.widget.LinearLayout.onoutVertical(LinearLayout.java:1557)的android.widget.LinearLayout.onLayout(LinearLayout.java:1466)在android.view.View.layout(View.java:15596)在android .view.ViewGroup.lay在android.view.ViewGroup.layout的android.view.View.layout(View.java:15596)的android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:890)上的(ViewGroup.java:4966) (ViewGroup.java:4966)android.widget.FrameLayout.layout中的android.widget.FrameLayout.layoutChildren(FrameLayout.java:573),android.view.Loutout(FrameLayout.java:508)android.view.View.layout(View.java: 15596)在Android.widget.FrameLayout.onLayout(FrameLayout.java:508)的android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)的android.view.ViewGroup.layout(ViewGroup.java:4966)。在Android.widget.FrameLayout.onLayout的android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)的android.view.ViewGroup.layout(ViewGroup.java:4966)上的view.View.layout(View.java:15596) (FrameLayout.java:508)在android.view.Linout.setChildFrame(androidLayout.setChild)中的android.view.View.layout(ViewGroup.java:4966)上的android.view.View.layout(View.java:15596) 1703)在android.widget.LinearLayout.layoutVertical(线性Layout.java:1557)在Android.view.Loutout(DirectLayout.java:1466)android.view.View.layout(View.java:15596)android.view.ViewGroup.layout(ViewGroup.java:4966)的android.widget.LinearLayout.onLayout(LinearLayout.java:1466) )android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)在android.view.fallLayout.onLayout(FrameLayout.java:508)android.view.layout(View.java:15596)android.view .ViewGroup.layout(ViewGroup.java:4966)位于android.view.ViewRootImpl.adTraversals上的android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829) View.RootImpl.java:1054)在android.view.View.Lovn上运行的android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5779)。在android.view.Choreographer.doCallbacks(Choreographer)的android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767) .java:580)在android.view.Choreographer $ frameDisplayEventReceiver.run(Choreographer.java:753)的android.view.Choreographer.doFrame(Choreographer.java:550)
这似乎与样式有关,好像我从文本视图中删除了这个样式属性似乎工作正常。
我认为你回来的convertView
有时是null
,因为处理convertView很棘手。
确保你有价值。你可以使用像:
@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>
以上是关于具有自定义样式的TextView导致奇怪的膨胀异常的主要内容,如果未能解决你的问题,请参考以下文章