ActionButton というカスタム ボタンを作成しましたが、並列ビュー階層にある別のビューへの参照を取得しようとしています。を使おうと思ったのですfindViewById(int id)
が、取得し続けていたNullPointerExceptions
ので、 経由で RootView への参照を取得しgetRootView()
、そこから でビューを取得しようとしましたfindViewById(int id)
。問題はgetRootView
、レイアウトまたは null を返す代わりに、そのメソッドを呼び出した ActionButton を返すことです。
これが私のActionButtonで、参照を取得しようとしています:
public class ActionButton extends Button {
protected void onFinishInflate(){
super.onFinishInflate();
log(getRootView() == this) //true, what I don't understand...
ConnectionLayer connectionLayer = (ConnectionLayer) findViewById(R.id.connection_layer); //Returns null...
}
}
私のlayout.xmlファイルの概要:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
(much more xml elements)
<com.cae.design.reaction.ui.ActionButton
android:id="@+id/actionButton"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center_vertical" />
</LinearLayout>
<com.cae.design.reaction.ui.ConnectionLayer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/connection_layer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.cae.design.reaction.ui.ConnectionLayer>
</FrameLayout>
getRootView
ビュー自体を返す理由を説明していただければ幸いです。または、他の方法で参照する方法のヒントを教えていただければ幸いです。