我创建了一个如下表单(form.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/formLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android :text="Name" />
<EditText
android:id="@+id/edName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- Many more things-->
</RelativeLayout>
如果上面的表单是通过代码创建的(比如 FormView),那么在每个活动中我都可以使用类似的东西
<com.example.FormView
android:id="@+id/fv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
但是由于表单是使用 xml 创建的,如上所示,我如何在众多活动中使用它?
我想在所有活动中使用上面的表单布局(form.xml)作为用户控件。
我尝试了以下操作,但它NullPointerException
在最后一行抛出:
setContentView(R.layout.main);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.container);
View form= (View) findViewById(R.id.formLayout);
mainLayout.addView(form);
任何建议表示赞赏。