Java (Android) 中的 findViewByID 相对布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16944145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
findViewByID Relative Layout in Java (Android)
提问by user2442455
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:
在我的 XML 中,我有一个包含两个相对布局(一个在顶部,一个在底部)的线性布局。底部的相对布局包含一个 ViewFlipper,我想用它来显示图像。现在,我正在尝试将这些图像放在 Java 中的文件列表中,以便我可以在“for 循环”中访问它们(如果有意义的话)。我正在尝试在 Java 中定义底部相对布局,以便我可以将 ViewFlipper 放入其中。当我尝试使用 findViewByID 时,我为布局创建的 ID(XML 格式)不会作为选项弹出。我是在以正确的方式进行这个过程,还是有另一种方法可以尝试?这是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>
回答by Houcine
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
方法setContentView(int resId)
缺失;所以你试图View
从一个空的 Content 中找到一个,这就是为什么它会给你一个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);
}