2014-11-22 41 views
0

我嘗試在另一個佈局中將項目添加到微調器,但它不成功。將項目添加到微調不同佈局中不起作用

這是我的代碼:

 View inflatedView = getLayoutInflater().inflate(R.layout.register, null); 
     Spinner spinner = (Spinner) inflatedView.findViewById(R.id.snprCity); 
     ArrayAdapter<String> adapter1; 
     List<String> list; 

     list = new ArrayList<String>(); 
     list.add("NY"); 
     list.add("CA"); 
     list.add("TX"); 
     list.add("CH"); 
     list.add("NJ"); 
     adapter1 = new ArrayAdapter<String>(getApplicationContext(), 
       android.R.layout.simple_spinner_item, list); 
     adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(adapter1); 
     setContentView(R.layout.register); 

註冊XML:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" android:weightSum="1" > 



    <ScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
     <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="30dp" 
       > 

      <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="نام :" 
        android:id="@+id/tmp" android:layout_gravity="right" android:paddingTop="30px" 
        android:paddingBottom="10px" 
        /> 
      <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textPersonName" 
        android:ems="10" 
        android:background="#ffffff" 
        android:textColor="#ff010101" 
        android:id="@+id/edtName" android:layout_gravity="right" /> 
      <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 

         android:text="نام خانوادگی: " 
         android:id="@+id/textView2" android:layout_gravity="right" android:paddingTop="30px" 
         android:paddingBottom="10px" 
         android:textColor="#ffffff"/> 
      <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textPersonName" 
        android:ems="10" 
        android:background="#ffffff" 
        android:textColor="#ff010101" 
        android:id="@+id/edtFamily" android:layout_gravity="right"/> 
      <TextView android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="رایانامه (ایمیل) :" 
         android:id="@+id/textView3" android:layout_gravity="right" android:paddingTop="30px" 
         android:paddingBottom="10px" 
         android:textColor="#ffffff"/> 
      <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="textEmailAddress" 
        android:ems="10" 
        android:background="#ffffff" 
        android:textColor="#ff010101" 
        android:id="@+id/edtEmail" android:layout_gravity="center_horizontal"/> 
      <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="شماره همراه :" 
         android:id="@+id/textView4" android:layout_gravity="right" android:paddingTop="30px" 
         android:paddingBottom="10px" 
         android:textColor="#ffffff"/> 
      /> 
      <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="phone" 
        android:ems="10" 
        android:background="#ffffff" 
        android:textColor="#ff010101" 
        android:id="@+id/edtTel" android:layout_gravity="center_horizontal"/> 
      <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="شهر: " 
         android:id="@+id/textView5" android:layout_gravity="right" 
         android:paddingTop="30px" 
         android:paddingBottom="10px" 
         android:textColor="#ffffff"/> 
      <Spinner 
        android:id="@+id/snprCity" 
        android:layout_width="match_parent" 
        android:layout_height="30dp" 
        android:focusable="true" 
        android:singleLine="true" 
        /> 
      <Button 
        android:layout_width="204dp" 
        android:layout_height="wrap_content" 
        android:text="ثبت نام" 
        android:id="@+id/btnRegiter" android:layout_marginTop="20dp" 
        android:onClick="btnRegiter" 
        /> 

     </LinearLayout> 
    </ScrollView> 


</LinearLayout> 

當代碼運行snprCity是空的。

我該如何解決?

+0

你的代碼看起來不錯。問題可以在其他地方嗎?可能(對不起,這樣一個簡單的例子)你膨脹了,但不要將你的視圖添加到它的父項,只看另一個微調? – sberezin 2014-11-22 20:24:29

+0

我發現了這個問題,但無法解決它,沒有找到'snprCity'。 – 2014-11-22 21:10:08

+0

如果未找到,則微調器將爲空,並且spinner.setAdapter()將導致異常。檢查雙ID ... – sberezin 2014-11-22 21:39:41

回答

0

我發現這個問題:

setContentView(R.layout.register); 

更改爲:

setContentView(inflatedView); 

與景觀的問題。

+0

這行不在你原來的文章中。所以這真的是關於通貨膨脹後的看法,正如我在11月23日發表的評論中提到的那樣 – sberezin 2014-11-27 17:30:47

+0

@sberezin是的我同意 – 2014-11-28 00:05:41

相關問題