首先,是的,我读了关于这个话题的所有其他帖子。不仅仅是这个网站的人…(你看,我有点沮丧)

大多数都建议使用android:id,而不是XML文件中的id。我做到了。

我从别人那里了解到,这种观点。findViewById的工作方式与Activity.findViewById不同。我也处理了。

在我的location_layout.xml中,我使用:

<FrameLayout .... >
    <some.package.MyCustomView ... />

    <LinearLayout ... >
        <TextView ...
            android:id="@+id/txtLat" />
        ...
    </LinearLayout>
</FrameLayout>

在我的活动中,我做:

...
setContentView( R.layout.location_layout );

在我的自定义视图类中:

... 
TextView tv = (TextView) findViewById( R.id.txtLat );

返回null。这样做,我的Activity工作得很好。所以这可能是因为活动。findViewById和View。findViewById差异。所以我存储上下文传递给本地的海关视图构造函数,并尝试:

...
TextView tv = (TextView) ((Activity) context).findViewById( R.id.txtLat );

它也返回null。

然后,我改变了我的自定义视图扩展ViewGroup而不是视图,并改变了location_layout.xml,让TextView是我的自定义视图的直接子,以便视图。findViewById应该正常工作。令人惊讶的是:它并没有解决任何问题。

我到底做错了什么?

欢迎任何意见。


当前回答

在我的情况下,我使用ExpandableListView,我设置了android:transcriptMode="normal"。这导致可扩展组中的几个孩子消失,我习惯了在我使用滚动列表时得到NULL异常。

其他回答

我也有同样的问题。我使用了一个第三方库,它允许你覆盖GridView的适配器,并为每个GridView单元指定自己的布局。

我终于意识到发生了什么。Eclipse仍然在为GridView中的每个单元格使用库的布局xml文件,尽管它没有给出这一点指示。在我的自定义适配器中,它表明它正在使用来自我自己项目的xml资源,尽管在运行时并不是这样。

所以我所做的是确保我的自定义xml布局和id与那些仍然坐在库中,清理项目,然后它开始读取正确的自定义布局,在我的项目中。

简而言之,如果要覆盖第三方库的适配器并指定自己的布局xml供适配器使用,请小心。如果项目中的布局与库中的布局具有相同的文件名,那么您可能会遇到一个非常难以找到的错误!

我只是想把我的具体案子提出来。也许能帮到别人。

我使用的指令在我的Android UI XML像这样:

父视图:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:tag="home_phone"
    android:background="@color/colorPrimary">

    ...

    <include
        layout="@layout/retry_button"
        android:visibility="gone" />

子视图(retry_button):

<com.foo.RetryButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/retry"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:layout_width="100dp"
    android:layout_height="140dp">

. findviewbyid (R.id.retry)将总是返回null。但是,如果我把ID从子视图移到include标签,它就开始工作了。

固定的家长:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:tag="home_phone"
    android:background="@color/colorPrimary">

    ...

    <include
        layout="@layout/retry_button"
        android:id="@+id/retry"
        android:visibility="gone" />

固定的孩子:

<com.foo.RetryButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:layout_width="100dp"
    android:layout_height="140dp">

我的解决办法是清理项目。

除了以上的解决方案,你要确保 工具:上下文= "。TakeMultipleImages” 在布局中与mainfest.xml文件中的值相同: android: name = "。TakeMultipleImages”用于相同的活动元素。 当使用复制粘贴来创建新活动时,就会发生这种情况

对于那些使用ExpandableListView并根据它的标题遇到这个问题的人来说,这是一个答案。

我有这个错误试图与TextViews在我的孩子和组视图作为一个expandabelistview实现的一部分。

您可以在getChildView()和getGroupView()方法的实现中使用如下内容。

        if (convertView == null) {
            LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_layout, null);
        }

我在这里找到了这个。