Android 从父视图获取xml布局

Android 从父视图获取xml布局,android,android-videoview,android-custom-view,Android,Android Videoview,Android Custom View,我有一个游戏XML布局,它扩展了视图,但是我不确定如何在游戏视图中获取XML布局值,请参见下面的代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.llpartners.sanctus.Game

我有一个
游戏
XML布局,它扩展了
视图
,但是我不确定如何在
游戏视图
中获取XML布局值,请参见下面的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.llpartners.sanctus.GameView 
        android:id="@+id/gameview" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/vidContainerBattle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="invisible" >
        <VideoView
            android:id="@+id/vvBeforeBattle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"/>
    </RelativeLayout>
</RelativeLayout>

在GameView类中使用以下代码

((Activity)context).findViewById(R.id.vvBeforeBattle);

您没有显示java代码,因此我必须在这里做一些假设。但是我相信,您得到空指针的原因是您没有首先设置
contentView
布局。在尝试
findViewById
之前,请将其添加到您的
onCreate()
函数中

setContentView(R.layout.myxmllayout);
编辑:我的错,我理解错了你的问题。如果您将您的
RelativeLayout
作为
GameView
的子项,您的问题应按以下方式解决:

<com.llpartners.sanctus.GameView 
        android:id="@+id/gameview" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/vidContainerBattle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="invisible" >
        <VideoView
            android:id="@+id/vvBeforeBattle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"/>
    </RelativeLayout>
</com.llpartners.sanctus.GameView>

此作业有很多选项:

1。在
GameView
view类中,访问其父类,然后获取所需视图的引用

((View) getParent()).findViewById(...);
2。如@zohreh所述,访问父级
活动
,然后使用其
findViewById
方法

3。在
GameView
类中添加一个类似
setVideoView()
的方法,然后在
活动
上下文中调用该方法,并将正确的引用传递给它


根据我的编程经验,第三种方法更好。

向我们展示访问这些对象的代码。了解您从何处访问它们也很重要。@land partners在下面看到我编辑的答案。这不是问题所有者所问的问题,他试图在视图子类中调用
findViewById
,而不是在activity@ABFORCE你完全正确,我的错。修正了我的回答以澄清问题。这种方法对我不起作用,因为我需要视频视图来使用家长的整个屏幕view@LandLPartners将布局高度也设置为填充父对象如何?我可以给你一些建议,通过你的java代码来实现这一点,但如果你能避免这一点,那将是非常理想的,因为它一点也不干净。我已经在我的问题中添加了java代码,谢谢。当通货膨胀结束时,你应该使用这些方法,一个选项是
onWindowFocusChanged()
回调方法,在你的
GameView
类的构造函数中,你不能使用这些方法。我制定了一个修复方案,我已经在视图中创建了一个公共静态,并将其绑定到父类中。您好,我尝试了上面的方法,但仍然得到了一个空指针。我已经在我的问题中添加了java代码,谢谢。
((View) getParent()).findViewById(...);