我是 android 新手,我正在尝试创建一个注册表单,用户应该能够添加多个用户。
我创建了一个包含表单的布局,并尝试在单击按钮时添加表单,它适用于第一次单击,并且应用程序在第二次单击时停止以添加更多表单,这是我的代码:
片段:
public static class NewCaseFragment extends Fragment {
public NewCaseFragment() {
}
//EditText orgname;
//EditText orgaddress ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.newcase,
container, false);
View formView = inflater.inflate(R.layout.mform,
container, false);
final LinearLayout formbis = (LinearLayout)rootView. findViewById(R.id.formbis);
final RelativeLayout form1 = (RelativeLayout)formView. findViewById(R.id.formm);
Button members= (Button)rootView. findViewById(R.id.members);
members.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout formtest = new RelativeLayout(v.getContext());
formtest = (RelativeLayout)form1;
//TextView tv1 = new TextView(v.getContext());
// tv1.setText("Show Up");
formbis.addView(formtest);
}
});
return rootView;
}
表单布局:
android:id="@+id/formm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<EditText
android:id="@+id/fname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/fname"
android:inputType="text"
android:textSize="12sp" />
<EditText
android:id="@+id/lname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fname"
android:hint="@string/lname"
android:inputType="text"
android:textSize="12sp" />
<Spinner
android:id="@+id/catspin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lname"
android:entries="@array/cat"
android:prompt="@string/cat"
android:textSize="10sp" />
<Spinner
android:id="@+id/rolespin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/catspin"
android:entries="@array/role"
android:prompt="@string/role"
android:textSize="10sp" />
<EditText
android:id="@+id/oparea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rolespin"
android:hint="@string/oparea"
android:inputType="text"
android:textSize="12sp" />
<EditText
android:id="@+id/job"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/oparea"
android:hint="@string/job"
android:inputType="text"
android:textSize="12sp" />
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/job"
android:hint="@string/phone"
android:inputType="phone"
android:textSize="12sp" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/phone"
android:hint="@string/email"
android:inputType="text"
android:textSize="12sp" />
</RelativeLayout>
新案例.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:orientation="horizontal"
android:padding="10dp" >
<EditText
android:id="@+id/casename"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:ems="10"
android:hint="@string/casename"
android:inputType="text"
android:textSize="12sp" >
<requestFocus />
</EditText>
</LinearLayout>
<!-- layout for organisation information -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="@+id/infoorg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1.0"
android:clickable="true"
android:hint="@string/infoorg"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:orientation="horizontal"
android:padding="10dp" >
<EditText
android:id="@+id/orgname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:hint="@string/orgname"
android:inputType="text"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:orientation="horizontal"
android:padding="10dp" >
<EditText
android:id="@+id/orgaddress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:hint="@string/orgaddress"
android:inputType="text"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="@+id/staff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1.0"
android:clickable="true"
android:hint="@string/staff"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="@+id/form1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<EditText
android:id="@+id/fname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/fname"
android:inputType="text"
android:textSize="12sp" />
<EditText
android:id="@+id/lname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fname"
android:hint="@string/lname"
android:inputType="text"
android:textSize="12sp" />
<Spinner
android:id="@+id/catspin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lname"
android:entries="@array/cat"
android:prompt="@string/cat"
android:textSize="10sp" />
<Spinner
android:id="@+id/rolespin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/catspin"
android:entries="@array/role"
android:prompt="@string/role"
android:textSize="10sp" />
<EditText
android:id="@+id/oparea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rolespin"
android:hint="@string/oparea"
android:inputType="text"
android:textSize="12sp" />
<EditText
android:id="@+id/job"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/oparea"
android:hint="@string/job"
android:inputType="text"
android:textSize="12sp" />
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/job"
android:hint="@string/phone"
android:inputType="phone"
android:textSize="12sp" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/phone"
android:hint="@string/email"
android:inputType="text"
android:textSize="12sp" />
</RelativeLayout>
<LinearLayout
android:id="@+id/formbis"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:orientation="horizontal"
android:padding="10dp" >
</LinearLayout>
<Button
android:id="@+id/members"
style="@style/btnStyleGrey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:drawableRight="@drawable/users2"
android:text="@string/plus" />
<Button
android:id="@+id/submit"
style="@style/btnStyleBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/submit" />
</LinearLayout>
</ScrollView>
这是日志:
05-11 16:17:25.947: W/dalvikvm(1614): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
05-11 16:17:25.987: E/AndroidRuntime(1614): FATAL EXCEPTION: main
05-11 16:17:25.987: E/AndroidRuntime(1614): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-11 16:17:25.987: E/AndroidRuntime(1614): at android.view.ViewGroup.addViewInner(ViewGroup.java:3344)
05-11 16:17:25.987: E/AndroidRuntime(1614): at android.view.ViewGroup.addView(ViewGroup.java:3215)
05-11 16:17:25.987: E/AndroidRuntime(1614): at android.view.ViewGroup.addView(ViewGroup.java:3172)
05-11 16:17:25.987: E/AndroidRuntime(1614): at android.view.ViewGroup.addView(ViewGroup.java:3152)
05-11 16:17:25.987: E/AndroidRuntime(1614): at com.pfe.risu.ManageActivity$NewCaseFragment$1.onClick(ManageActivity.java:207)
05-11 16:17:25.987: E/AndroidRuntime(1614): at android.view.View.performClick(View.java:3480)
05-11 16:17:25.987: E/AndroidRuntime(1614): at android.view.View$PerformClick.run(View.java:13983)
05-11 16:17:25.987: E/AndroidRuntime(1614): at android.os.Handler.handleCallback(Handler.java:605)
05-11 16:17:25.987: E/AndroidRuntime(1614): at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 16:17:25.987: E/AndroidRuntime(1614): at android.os.Looper.loop(Looper.java:137)
05-11 16:17:25.987: E/AndroidRuntime(1614): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-11 16:17:25.987: E/AndroidRuntime(1614): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 16:17:25.987: E/AndroidRuntime(1614): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 16:17:25.987: E/AndroidRuntime(1614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-11 16:17:25.987: E/AndroidRuntime(1614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-11 16:17:25.987: E/AndroidRuntime(1614): at dalvik.system.NativeStart.main(Native Method)