我想通过单击Image动态地生成一个按钮,在.java中我提到了Image,并且在类中也调用了它。我的xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/theme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageButton android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/plus1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcomemilan" />
</LinearLayout>我的java类如下所示:公共类MainActivity扩展活动{
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//to access linear layout
final LinearLayout l = (LinearLayout)findViewById(R.id.theme);
// my add button image
ImageButton b = (ImageButton)findViewById(R.id.button);
//to generate a new button
final Button b1 = new Button(this);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// the button is clicked now i need to generate a button
for(int j =0; j<10;j++){
b1.setText("Add");
l.addView(b1);
Log.i(TAG, "the button is pressed"+j);
}
}
});
}
}到现在为止一切都很好。我得到的按钮和看到的图像,但当按下按钮,我得到致命的例外,像这样。请在这方面给予帮助,我是android的新手。
07-03 10:31:46.853: E/AndroidRuntime(639): FATAL EXCEPTION: main
07-03 10:31:46.853: E/AndroidRuntime(639): java.lang.IllegalStateException:The specified child already has a parent. You must call removeView() on the child's parent first.
07-03 10:31:46.853: E/AndroidRuntime(639): at android.view.ViewGroup.addViewInner(ViewGroup.java:3344)
07-03 10:31:46.853: E/AndroidRuntime(639): at android.view.ViewGroup.addView(ViewGroup.java:3215)
07-03 10:31:46.853: E/AndroidRuntime(639): at android.view.ViewGroup.addView(ViewGroup.java:3172)
07-03 10:31:46.853: E/AndroidRuntime(639): at android.view.ViewGroup.addView(ViewGroup.java:3152)
07-03 10:31:46.853: E/AndroidRuntime(639): at com.pro.raisebutton.MainActivity$1.onClick(MainActivity.java:38)
07-03 10:31:46.853: E/AndroidRuntime(639): at android.view.View.performClick(View.java:3480)
07-03 10:31:46.853: E/AndroidRuntime(639): at android.view.View$PerformClick.run(View.java:13983)
07-03 10:31:46.853: E/AndroidRuntime(639): at android.os.Handler.handleCallback(Handler.java:605)
07-03 10:31:46.853: E/AndroidRuntime(639): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 10:31:46.853: E/AndroidRuntime(639): at android.os.Looper.loop(Looper.java:137)
07-03 10:31:46.853: E/AndroidRuntime(639): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-03 10:31:46.853: E/AndroidRuntime(639): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 10:31:46.853: E/AndroidRuntime(639): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 10:31:46.853: E/AndroidRuntime(639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-03 10:31:46.853: E/AndroidRuntime(639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-03 10:31:46.853: E/AndroidRuntime(639): at dalvik.system.NativeStart.main(Native Method)请在这方面提供帮助。
发布于 2014-07-03 10:58:07
您的布局已经包含了该按钮。所以你才会得到这个例外。
如果要向布局动态添加按钮,则必须在每次迭代时创建该按钮的新实例:
for(int j =0; j<10;j++){
Button newButton = new Button(MainActivity.this);
l.addView(newButton);
}或者,您可以创建一个包含按钮的布局,其中包含您喜欢的布局参数,并在每次迭代时使用一个充气器来获取按钮的一个新实例。
我注意到你的布局是水平的,这就是你想要的吗?
发布于 2014-07-03 11:03:30
java.lang.IllegalStateException:The指定的子对象已经具有父级。您必须首先对孩子的父级调用removeView()。
日志猫中的这一行表示视图只能有一个父对象。因此,当您尝试在循环中添加b1 **多次**时,就会产生这个问题。为了克服这个问题,您需要为每个循环迭代拥有一个按钮的单独实例。
像这样的事情应该能解决:
for(int j =0; j<10;j++)
{
Button b1 = new Button(getApplicationContext());
b1.setText("Add");
l.addView(b1);
Log.i(TAG, "the button is pressed"+j);
}发布于 2014-07-03 10:57:59
您可以使用此代码添加一个动态按钮。
Button myButton = new Button(this);
myButton.setText("Add Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.theme);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);将此代码放入onclick函数并进行调整。
https://stackoverflow.com/questions/24551581
复制相似问题