简体   繁体   中英

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). The bottom Relative Layout contains a ViewFlipper, which I want to use to show images. 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. I'm trying to define the bottom Relative layout in Java, so that I can put the ViewFlipper in it. When I try to use findViewByID, the ID I created for the layout (in XML) doesn't pop up as an option. 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:

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

My 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; so you are trying to find a View from a null Content , that's why it will give you a 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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