android - 偶尔从 (View).findViewByID 获取 null

标签 android android-arrayadapter findviewbyid

我在 Android Market 上有一个 Android 应用程序。我不断收到一些崩溃报告,声称某些用户收到 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>

最佳答案

我在已发布的应用程序中也遇到过这个问题。 经过大量的 self 反省后,我以两种不同的方式重新创建了它!

原因 1 ) 原来它是 null 的原因是当时没有内存可以分配它。只需要编写更多保护代码并关闭。

原因 2) 在某些情况下,Dalvik 确信我没有使用它并释放它。我不得不更改我的代码,以确保它在 Dalvik 眼中更“紧密相连”。

然而,10 次中有 9 次是原因 1。

问题是你无法重现它,因为你正在使用测试设备,其他人同时运行各种东西。

关于android - 偶尔从 (View).findViewByID 获取 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8431618/

相关文章:

android - ActionBar获取当前图标,findviewbyid

android - 在抽屉导航中切换 fragment 时短黑闪烁

android - Android将特定的构建警告视为错误,因此在Crashlytics映射文件上传失败时构建失败

android - java.lang.IllegalAccessError : Method 'void android. support.v4.content

android - 未调用 OnScrollChanged 状态

android - startActivityForResult 使用 ArrayAdapter 将数据传递到 "toActivityResult"

android - 非法反射访问操作

android - ListView 中的可见性消失了

android - 如何在 Android 中使用 View 绑定(bind)

java - findViewById 从 Activity 返回 null