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

大多数都建议使用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应该正常工作。令人惊讶的是:它并没有解决任何问题。

我到底做错了什么?

欢迎任何意见。


当前回答

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

其他回答

调试和查找问题的方法:

注释掉活动中的所有findViewById。 注释掉所有的东西,除了onCreate和setContentView 运行项目并查看是否设置了任何布局

在我的情况下,我使用activity_main.xml在我的应用程序模块和我的库模块。所以当我执行上面的步骤,而不是我在库中设计的布局,app模块内的布局是膨胀的。

因此,我将activity_main_xml文件名称改为activity_main_lib.xml。

因此,请确保在整个项目中没有任何重复的布局名称。

我试过以上所有的方法,但都没有效果。所以我必须让我的ImageView static public static ImageView texture;然后texture = (ImageView) findViewById(R.id.texture_back);,我不认为这是一个好方法,但这确实对我的情况有效:)

我在两个单独的模块中有相同的.xml文件,其中有相同的视图id。

应用程序/ src / main / res / / header.xml布局 supportModule / src / main / res / layout / header.xml

两个布局都有相同的TextView id @+id/tv_prompt。

我的TextView tvPrompt = findViewById(R.id.tv_prompt);总是返回null。为了解决这个问题,我不得不将另一个xml文件重命名为header_main.xml。

膨胀布局!!(包含id)

在我的例子中,findViewById()返回null,因为元素被写入的布局没有被膨胀…

如。 fragment_layout.xml

<ListView
android:id="@+id/listview">

findViewById(R.id.listview)返回null,因为我没有做 inflater.inflate (R.layout.fragment_layout……,……); 在这之前。

希望这个答案能帮助到你们。

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

我使用的指令在我的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">