Android findViewById 不适用于包含?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10803672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:01:22  来源:igfitidea点击:

findViewById not working for an include?

android

提问by user291701

I have an include like:

我有一个包括:

<include
    android:id="@+id/placeHolder"
    layout="@layout/test" />

The include layout looks like (test.xml):

包含布局看起来像(test.xml):

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/outer"
    ... >

    <ImageView
        android:id="@+id/inner"
        ... />
</FrameLayout>

I can't seem to find the inner ImageView with id="inner" at runtime:

我似乎无法在运行时找到 id="inner" 的内部 ImageView:

ImageView iv = (ImageView)findViewById(R.id.inner);
if (iv == null) {
    Log.e(TAG, "Not found!");
}

Should I be able to find it? It seems like since it's using an "include", the normal findViewById method does not work.

我应该能找到它吗?似乎因为它使用了“包含”,所以正常的 findViewById 方法不起作用。

---------- Update ----------------

- - - - - 更新 - - - - - - - -

So I can find the id assigned to the include:

所以我可以找到分配给包含的 id:

View view = findViewById(R.id.placeHolder); // ok

but I can't find any of its children by id like:

但我无法通过 id 找到它的任何孩子,例如:

view.findViewById(R.id.outer); // nope
view.findViewById(R.id.inner); // nope

same as the original if I try searching for them directly like:

如果我尝试直接搜索它们,则与原始内容相同:

findViewById(R.id.outer); // nope
findViewById(R.id.inner); // nope

Do ids just get stripped off of elements at runtime maybe?

id 是否只是在运行时从元素中剥离?

Thanks

谢谢

回答by dymmeh

Try retrieving the <include />and then searching within that

尝试检索<include />然后在其中搜索

Make sure your root has the same ID as the root element in the included XML file.. ex

确保您的根与包含的 XML 文件中的根元素具有相同的 ID.. 例如

<include
    android:id="@+id/outer"
    layout="@layout/test" />

Then retrieve your "inner" content using:

然后使用以下方法检索您的“内部”内容:

FrameLayout outer = (FrameLayout)findViewById(R.id.outer);

ImageView iv = (ImageView)outer.findViewById(R.id.inner);
if (iv == null) {
    Log.e(TAG, "Not found!");
}

回答by Daniel Wilson

I just wanted to add to this for future Googlers, I had kind of the opposite problem where the root layout of the included xml (in this case outer), was null when calling findViewById(), but the children of outer were not null.

我只是想为未来的 Google 员工添加这个,我遇到了相反的问题,其中包含的 xml 的根布局(在这种情况下为外部)在调用 findViewById() 时为空,但外部的子项不为空。

The problem was solved for me by removing the id property of the include'view' (in this case placeHolder). When that was gone, I could find outer but it's id :)

通过删除include“视图”的 id 属性(在本例中为 placeHolder),我的问题得到了解决。当它消失时,我可以找到外部但它是 id :)

If you need a an id assigned to the includeitem, I think it's better to wrap it in another viewgroup.

如果您需要为该include项目分配一个 id ,我认为最好将其包装在另一个视图组中。

回答by HungNM2

I had the same problem. I found out that I used a wrong function with the same name with 2 parameters as following:

我有同样的问题。我发现我使用了一个带有 2 个参数的同名函数,如下所示:

public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.activity_ble_connecting);
    //findViewByid() => wrong
} 

correctfunction with only one parameters:

只有一个参数的正确函数:

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ble_connecting);
        //findViewByid() => correct
    }

回答by Beto

Future researchers: https://stackoverflow.com/a/1760010/2790016when you add an id to it overwrites the id from the outer tag from the "real layout"

未来的研究人员:https: //stackoverflow.com/a/1760010/2790016当您向其添加 id 时,它会覆盖来自“真实布局”的外部标签中的 id

Given that most constraint layouts need an id to positions items, removing the id from include is not an ideal answer.

鉴于大多数约束布局需要一个 id 来定位项目,从包含中删除 id 不是一个理想的答案。

回答by Beto

If you have given the id to the includeelement and you have only single view in the included layout (not in any ViewGroup), in my case AdView, then use the id of includeelement instead of included view.

如果您已经为include元素提供了 id,并且在包含的布局中只有一个视图(不在任何ViewGroup),那么在我的情况下AdView,请使用include元素的 id而不是包含的视图。

<?xml version="1.0" encoding="utf-8"?>
<!--adview.xml-->
<com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/admob_banner_id">
</com.google.android.gms.ads.AdView>

activity_main.xml

活动_main.xml

<include
        layout="@layout/adview"
        android:id="@+id/adsLayout"
  />

In activity

活动中

AdView nullAdView=findViewById(R.id.adView); //this will give null
AdView adView = findViewById(R.id.adsLayout);  //this will be AdView

If you do not give idto includeelement then it is ok to use view id. Also if your viewis inside any ViewGrouplike FrameLayoutthen it is ok to use view id.

如果你不给idinclude元素,则它是确定使用视图id。此外,如果您view在任何ViewGroup类似的内部FrameLayout,则可以使用 view id

回答by Andi Tenroaji Ahmad

in my case, inisialisasi your include must same your root view include .

就我而言, inisialisasi 您的 include 必须与您的根视图 include 相同。

in include:

其中包括:

<include
    android:id="@+id/placeHolder"
    layout="@layout/test" />

in view include:

考虑到包括:

  <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/outer"
        ... >

        <ImageView
            android:id="@+id/inner"
            ... />
    </FrameLayout>

in activity :

在活动中:

 FrameLayout placeHolder = findViewById(R.id.placeHolder);

    ImageView iv = findViewById(R.id.inner);

so id in include replace id in root view include

所以包含中的 id 替换根视图中的 id 包含

回答by Felipe Caldas

Can you try then finding the outer parent (lets call it 'op') and then try op.findViewById() ?

您可以尝试找到外部父级(我们称之为“op”)然后尝试 op.findViewById() 吗?