I want to generate a button Dynamically by clicking on an Image Button, in XML i mentioned the Image Button and i also called it in .java class. My xml is as follows :
<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>
My java class for this is as follows public class MainActivity extends Activity {
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);
}
}
});
}
}
Till now all were fine. I am getting the button and seeing the image but when press the button i am getting fatal Exception like this. kindly help in this regard, i am new to 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)
Kindly help in this regard