I have a LinearLayout
and I want to add my new programatically added CardView
inside it as a child using the LayoutInflater
llMain = (LinearLayout)findViewById(R.id.llMain);
LayoutInflater inflater = getLayoutInflater();
Bundle extras = getIntent().getExtras();
if (extras != null) {
final TextView tvAdded = new TextView(this);
CardView cvAdded = new CardView(this);
tvAdded.setTextSize(18);
tvAdded.setTextColor(Color.BLACK);
tvAdded.setClickable(true);
tvAdded.setPadding(0, 10, 0, 0);
tvAdded.setGravity(Gravity.CENTER);
String value = extras.getString("etTest");
tvAdded.setText(value);
cvAdded = (CardView) inflater.inflate((XmlPullParser) llMain, cvAdded, false);
cvAdded.addView(tvAdded);
cvAdded.setLayoutParams(new CardView.LayoutParams(CardView.LayoutParams.MATCH_PARENT, CardView.LayoutParams.MATCH_PARENT));
llMain.addView(cvAdded);
}
The output I want should be the CardView
shows the width and height of match parent, which is the llMain
but the problem is that, when I run it, it crashes the app. This is the log of the crash
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wordpress.fidzsoftware/com.wordpress.fidzsoftware.MainActivity}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to org.xmlpull.v1.XmlPullParser
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to org.xmlpull.v1.XmlPullParser
at com.wordpress.fidzsoftware.MainActivity.onCreate(MainActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)