我正在尝试进行动态布局,其中有一个滚动视图。 在滚动视图中,我添加了引用main.xml的垂直线性布局。 代码会立即停止,我尝试添加引用main.xml的布局。
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>
答案 0 :(得分:1)
尝试添加
setContentView(R.layout.main);
之前:
vt=(LinearLayout) findViewById(R.id.llv);
答案 1 :(得分:0)
试试这个:
view row = LayoutInflater.from(this).inflate(R.layout.main, null);
LinearLayout layout=row.findViewById(R.id.YourLayout);
答案 2 :(得分:0)