2012-05-14 39 views
0

我想做動態佈局,其中有滾動視圖。 在滾動視圖中,我添加了引用main.xml的垂直線性佈局。 代碼儘快停止,我嘗試添加引用main.xml的佈局。無法加載線程佈局從main.xml在dyanimic佈局

ScrollView scroll; 
    EditText search; 
    Button checkin; 
     LinearLayout vt; 

     public void onCreate(Bundle savedInstanceState) 
     { 

      super.onCreate(savedInstanceState); 
      scroll=new ScrollView(getApplicationContext()); 
      vt=(LinearLayout) findViewById(R.id.llv);///referencing linear layout located in main.xml.This is where problem the is occurring. 
      //vt.setOrientation(LinearLayout.VERTICAL); 
      scroll.addView(vt); 
      this.setContentView(scroll); 
     } 

main.xml中(其中R.id.llv所在地)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 



    <LinearLayout 
     android:id="@+id/llv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 
     <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

     <ToggleButton 
      android:id="@+id/toggleButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="ToggleButton" /> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 
    </LinearLayout> 

</LinearLayout> 

回答

1

嘗試添加

setContentView(R.layout.main); 

前:

vt=(LinearLayout) findViewById(R.id.llv); 
+0

你是錯了。 我想在dyanimic佈局中從main.xml添加線性佈局。 –

+0

@ShoaibKhan你需要先膨脹你的xml。 –

0

試試這個:

view row = LayoutInflater.from(this).inflate(R.layout.main, null); 
LinearLayout layout=row.findViewById(R.id.YourLayout); 
+0

super.onCreate(savedInstanceState); View row = LayoutInflater.from(this).inflate(R.layout.main,null); 查看vt = row.findViewById(R.id.llv); scroll = new ScrollView(getApplicationContext()); scroll.addView(vt); this.setContentView(scroll); 這是我在做什麼。但仍然強制關閉 –