简体   繁体   中英

About adding cardview programatically inside linear layout

I have to create a case where I need to insert 5 card-like structure, inside my linearlayout present in activity_main.xml

activity_main layout is like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

</LinearLayout>

And the cardview layout is like this :

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    android:layout_height="100dp"
    android:layout_width="250dp"
    android:background="@color/white"
    app:cardCornerRadius="40dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/title"
        android:text="Title from Android"
        android:textAlignment="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="monospace"/>
    <TextView
        android:id="@+id/desc"
        android:layout_marginTop="40dp"
        android:textAlignment="center"
        android:text="adding layout dynamically"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fontFamily="serif"/>
</androidx.cardview.widget.CardView>

And on mainactivity onCreate() method I have:

super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout parent=(LinearLayout) findViewById(R.id.viewMain);
        LayoutInflater inflater=getLayoutInflater();

        View view=inflater.inflate(R.layout.text_main,parent,true);

        for(int i=0;i<3;i++){
            parent.addView(view,i);  
        }

    }

Error shown is like:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workout/com.example.workout.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3735) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3903) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2328) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:255) at android.app.ActivityThread.main(ActivityThread.java:8212) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632) at 881689 70007088.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049) Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:5377) at android.view.ViewGroup.addView(ViewGroup.java:5192) at android.view.ViewGroup.addView(ViewGroup.java:5132) at com.example.workout.MainActivity.onCreate(MainActivity.java:36) at android.app.Activity.performCreate(Activity.java:8151) at android.app.Activity.performCreate(Activity.java:8135) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3704) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3903) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertra nsaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2328) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:255) at android.app.ActivityThread.main(ActivityThread.java:8212) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)

Can anybody please help me how can I fix this, so that I can dynamically insert 5 card like structures inside my linear layout`

Change

LayoutInflater inflater=getLayoutInflater(); 
View view=inflater.inflate(R.layout.text_main,parent,true);

to

View view = LayoutInflater.from(this).inflate(R.layout.text_main, null);

and add LayoutInflater code in for loop

Add following code in MainActivity.java

LinearLayout parent=(LinearLayout) findViewById(R.id.viewMain);
for (int i = 0; i < 3; i++) {
    View view = LayoutInflater.from(this).inflate(R.layout.text_main, null);
    TextView title =  view.findViewById(R.id.title);
    TextView desc =  view.findViewById(R.id.desc);
    parent.addView(view);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM