我正在尝试在其中一个应用程序页面上动态添加一个按钮。我收到以下错误:
01-04 13:14:28.733: E/AndroidRuntime(811): FATAL EXCEPTION: main
01-04 13:14:28.733: E/AndroidRuntime(811): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wb/com.example.wb.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-04 13:14:28.733: E/AndroidRuntime(811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.os.Looper.loop(Looper.java:137)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-04 13:14:28.733: E/AndroidRuntime(811): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 13:14:28.733: E/AndroidRuntime(811): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 13:14:28.733: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-04 13:14:28.733: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-04 13:14:28.733: E/AndroidRuntime(811): at dalvik.system.NativeStart.main(Native Method)
01-04 13:14:28.733: E/AndroidRuntime(811): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-04 13:14:28.733: E/AndroidRuntime(811): at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.view.ViewGroup.addView(ViewGroup.java:3210)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.view.ViewGroup.addView(ViewGroup.java:3155)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.view.ViewGroup.addView(ViewGroup.java:3131)
01-04 13:14:28.733: E/AndroidRuntime(811): at com.example.wb.MainActivity.onCreate(MainActivity.java:63)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.app.Activity.performCreate(Activity.java:5104)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-04 13:14:28.733: E/AndroidRuntime(811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-04 13:14:28.733: E/AndroidRuntime(811): ... 11 more
这是我要添加的活动
public class MainActivity extends Activity {
EditText text1;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int MY_BUTTON = 9000;
Bundle extras = getIntent().getExtras();
String userid = extras.getString("userid");
//LayoutInflater factory = getLayoutInflater();
LinearLayout ll = (LinearLayout)findViewById(R.id.linear);
Button b = new Button(this);
b.setText("Button added dynamically!");
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
b.setId(MY_BUTTON);
ll.addView(b);
/*UserLoginTask mAuthTask = new UserLoginTask();
String urlwithget="?userid="+userid+"&task=alltitles";
mAuthTask.execute(urlwithget);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL); //Can also be done in xml by android:orientation="vertical"
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button 1");
btnTag.setId(2);
row.addView(btnTag);
*/
}
以下是我对此活动的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="onClick"
android:text="@string/logout" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="19dp"
android:layout_toLeftOf="@+id/button1"
android:onClick="goOn"
android:text="@string/forward" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="36dp"
android:ems="10"
android:text="@string/vishal" />
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginLeft="60dp"
android:layout_marginTop="58dp"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
问题是什么?