偶尔从 (View).findViewByID 获取 null
我有一个 Android 应用程序 @ Android Market。我不断收到一些崩溃报告,声称某些用户遇到 NullPointerException。
我已经找到了似乎导致此问题的代码。
它位于 ArrayAdapters getView 方法中。
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.subcatategory_row, null);
holder = new ViewHolder();
v.setTag(holder);
}
else {
holder=(ViewHolder)v.getTag();
if(holder == null){
holder = new ViewHolder();
}
}
TextView tl = (TextView) v.findViewById(R.id.label);
tl.setTextAppearance(getContext(), R.style.styleA);
该片段中的最后一行似乎导致了崩溃。不过我从未体验过它,并且我已经在多个设备上进行了测试。
帮助将不胜感激!
编辑:
subcategory_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dip"
android:background="@drawable/background"
android:weightSum="80">
<TextView
android:id="@+id/label"
android:layout_weight="9"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:gravity="center_vertical|center"
android:text="C.r"
android:textSize="12sp"
android:textColor="@color/default_textcolor"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="53"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text_bb"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_marginLeft="4dip"
android:gravity="bottom"
android:textSize="13sp"
android:textColor="@color/default_textcolor"
android:ellipsize="end" android:singleLine="true"/>
<TextView
android:id="@+id/text_ba"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_marginLeft="4dip"
android:gravity="top"
android:textSize="13sp"
android:textColor="@color/default_textcolor"
android:ellipsize="end" android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="8"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text_a"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="bottom|right"
android:textSize="13sp"
android:text="0"
android:textColor="@color/default_textcolor"
/>
<TextView
android:id="@+id/text_b"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="top|right"
android:textSize="19sp"
android:text="0"
android:textColor="@color/default_textcolor"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_weight="10"
android:orientation="vertical"
android:paddingLeft="10dp" >
<ImageView
android:id="@+id/arrow"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:paddingLeft="-10dip"
android:paddingRight="10dip"
android:src="@drawable/arrow"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
I hava a Android app @ Android Market. And Im keep getting some crash reports that claims that some users get a NullPointerException.
I've located the code that seems to be causing this problem.
It's in a ArrayAdapters getView method.
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.subcatategory_row, null);
holder = new ViewHolder();
v.setTag(holder);
}
else {
holder=(ViewHolder)v.getTag();
if(holder == null){
holder = new ViewHolder();
}
}
TextView tl = (TextView) v.findViewById(R.id.label);
tl.setTextAppearance(getContext(), R.style.styleA);
The last line in this snippet seems to be causing the crash. However I've never experienced it, and I've been testing it on several devices.
Help would be greatly appreciated!!
EDIT:
subcategory_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dip"
android:background="@drawable/background"
android:weightSum="80">
<TextView
android:id="@+id/label"
android:layout_weight="9"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:gravity="center_vertical|center"
android:text="C.r"
android:textSize="12sp"
android:textColor="@color/default_textcolor"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="53"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text_bb"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_marginLeft="4dip"
android:gravity="bottom"
android:textSize="13sp"
android:textColor="@color/default_textcolor"
android:ellipsize="end" android:singleLine="true"/>
<TextView
android:id="@+id/text_ba"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_marginLeft="4dip"
android:gravity="top"
android:textSize="13sp"
android:textColor="@color/default_textcolor"
android:ellipsize="end" android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="8"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text_a"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="bottom|right"
android:textSize="13sp"
android:text="0"
android:textColor="@color/default_textcolor"
/>
<TextView
android:id="@+id/text_b"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="top|right"
android:textSize="19sp"
android:text="0"
android:textColor="@color/default_textcolor"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_weight="10"
android:orientation="vertical"
android:paddingLeft="10dp" >
<ImageView
android:id="@+id/arrow"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:paddingLeft="-10dip"
android:paddingRight="10dip"
android:src="@drawable/arrow"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在已发布的应用程序上也遇到了这个问题。
经过一番深思熟虑后,我重新创建了它——用两种不同的方式!
原因1)原来它是null的原因是当时没有内存来分配它。只需编写更多保护代码并关闭即可。
原因 2)Dalvik 在某些情况下确信我没有使用它并释放它。我必须改变我的代码,以确保它在达尔维克眼中更加“紧密相连”。
然而,十有八九是原因1。
问题是你无法重现它,因为你正在使用测试设备,其他人同时运行着各种各样的东西。
I also have had this issue on published apps.
After a lot of soul searching I recreated it - in two different ways !
Reason 1 ) Turns out the reason it is null , is that there was no memory at the time to allocate it. Just had to write more protective code and shut down.
Reason 2 ) Dalvik in some cases got convinced that I was not using it and deallocated it. I had to alter my code to make sure it was more "hooked together" in Dalvik's eye.
However 9 times out of 10 , it was reason 1.
The problem is that you can't reproduce it as you are using test devices , other people have all sorts of stuff running at the same time.
您应该将
TextView
存储在 ViewHolder 对象中这是我使用过的示例
You should store the
TextView
inside your ViewHolder objectHere's an example of something I've used