简体   繁体   English

Java中的findViewByID相对布局(Android)

[英]findViewByID Relative Layout in Java (Android)

In my XML, I have a Linear Layout that contains two Relative Layouts (one on top, one on bottom). 在我的XML中,我有一个线性布局,其中包含两个相对布局(一个在顶部,一个在底部)。 The bottom Relative Layout contains a ViewFlipper, which I want to use to show images. 底部的相对布局包含一个ViewFlipper,我想用它来显示图像。 Right now, I'm trying to place these images in a filelist in Java so I can access them in a 'for loop', if that makes sense. 现在,我正在尝试将这些图像放置在Java的文件列表中,这样我就可以在“ for循环”中访问它们。 I'm trying to define the bottom Relative layout in Java, so that I can put the ViewFlipper in it. 我正在尝试在Java中定义底部的相对布局,以便可以将ViewFlipper放入其中。 When I try to use findViewByID, the ID I created for the layout (in XML) doesn't pop up as an option. 当我尝试使用findViewByID时,我为布局创建的ID(以XML格式)不会作为选项弹出。 Am I going at the process the right way or is there another way I can try? 我是按照正确的方式进行操作还是可以尝试其他方法? This is that part of the Java code: 这是Java代码的一部分:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
brl = (RelativeLayout) findViewById(R.id.);
vf = (ViewFlipper) findViewById(R.id.Flipper);

My XML: 我的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Analysis"
    android:id="@+id/mainLL" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_gravity="top"
        android:id="@+id/topRL" >

        <TextView
            android:id="@+id/textViewFilename"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="16dp"
            android:gravity="center"
            android:text="Filename"
            android:textAlignment="center"
            android:textSize="35dp" />

        <TextView
            android:id="@+id/textViewSpecies"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textViewFilename"
            android:gravity="center"
            android:text="Frog Species"
            android:textSize="40dp" />

        <EditText
            android:id="@+id/editTextLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textViewSpecies"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="19dp"
            android:ems="10"
            android:hint="What is your location?" />

        <Button
            android:id="@+id/buttonSave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="SAVE" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:id="@+id/bottomRL" >

        <ViewFlipper
            android:id="@+id/Flipper"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true" >
        </ViewFlipper>
    </RelativeLayout>

</LinearLayout>

the method setContentView(int resId) is missing; 方法setContentView(int resId)丢失; so you are trying to find a View from a null Content , that's why it will give you a NullPointerException 因此,您尝试从null内容中查找View ,这就是为什么它将给您NullPointerException

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // IMPORTANT : set a content layout to your activity before retreiving any view
        setContentView(R.layout.activity_main);
        brl = (RelativeLayout) findViewById(R.id.);
        vf = (ViewFlipper) findViewById(R.id.Flipper);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM