2

ビューが表示されているかどうかを確認するコードがあります

import kotlinx.android.synthetic.main.activity_layout.*

val isOverflowPanelShown: Boolean
   get() = overflow_panel.visibility != View.GONE

前のコードは例外をスローします

java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.ScrollView
    at com.company.app.Activity.isOverflowPanelShown(Activity.kt:362)

ビューはScrollViewクラスのインスタンスですが、kotlin はそれをFrameLayout. エラーがスローされたのと同じ場所で findViewById() を呼び出すと、正しく ScrollView が返されます。アプリケーションの異なるレイアウトで、FrameLayout同じ id の下にあることがわかりました。

次のレイアウトを膨らませています

activity_layout

<ScrollView
    android:id="@+id/overflow_panel"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    />

まったく別の場所で使用する別のレイアウトには、同じ ID を持つ別のビューがあります。

form_component_main

<FrameLayout
    android:id="@+id/overflow_panel" 
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    />
4

1 に答える 1

2

それらに異なるIDを与えないのはなぜですか?

overflow_panel_scroll
overflow_panel_frame

または、彼らが実際に何をするかをより説明する何か。

更新:これは反対票を投じられたため、もう少し説明します。ID は一意である必要があります。

Android のドキュメントには、ID が一意でない場合に競合が発生する可能性があると記載されています: An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).( http://developer.android.com/guide/topics/ui/declaring-layout.htmlから)

Kotlin シンセティックは、IntelliJ プラグインによって生成されます。ID が一意でないと、プラグインは現在、ID を正しいビューに正しく一致させることができないようです。一意の ID が期待されている可能性があります。

于 2016-07-12T21:14:42.043 に答える