2013-06-05 53 views
0

在我的XML中,我有一個線性佈局,其中包含兩個相對佈局(頂部一個,底部一個)。底部的相對佈局包含一個ViewFlipper,我想用它來顯示圖像。現在,我試圖將這些圖像放在Java文件列表中,以便我可以在'for循環'中訪問它們,如果這樣做有道理的話。我試圖在Java中定義底部相對佈局,以便我可以將ViewFlipper放入其中。當我嘗試使用findViewByID時,我爲佈局創建的標識(XML)不會作爲選項彈出。 我是以正確的方式參與這個過程,還是有另一種方式可以嘗試? 這是Java代碼的一部分:findViewByID在Java中的相對佈局(Android)

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

我的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> 
+1

的setContentView(R.layout.mylayout); // missing brl =(RelativeLayout)findViewById(R.id.Relativelayout); – Raghunandan

+0

也許吧。你應該知道的一件事是findViewById只能找到CHILD視圖。那麼R.id.Flipper是運行上面的java代碼的視圖的子代? – MTilsted

+0

發佈xml。在那裏你錯過了佈局的setcontentview。 – soynerdito

回答

4

方法setContentView(int resId)丟失;所以你正在努力尋找從空內容一個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); 
} 
+0

我添加了setContentview,但相對佈局的ID仍不會作爲選項彈出。然而,通過你的回答,我可以安全地假設它有可能使用findViewByID進行佈局嗎? – user2442455

+1

是的。你當然可以。我們可以在'setContentView()'方法中使用 –

+0

,你應該把你的佈局的名稱作爲一個id(例如'setContentView(R.layout.your_xml_file_name);'),並且在你的Activity中,你可以通過它的id使用findViewById()方法(例如:'RelativeLayout topRL =(RelativeLayout)findViewById(R.id.topRL);') – Houcine