简体   繁体   中英

Android Studio Add text view on button click

So i'm trying to create a java app for tracking orders, and I need each button to create a view in an ordered list currently i'm just trying to get the first button to add information but it isn't doing anything. Any help would be appreciated as I am very new to android programming.

Section of code in activity main that contains the linear layout and button i want to be able to add views

 <Button android:id="@+id/add_funnel_cake" android:layout_width="90dp" android:layout_height="80dp" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:paddingRight="10dp" android:text="Funnel Cake"></Button>

Layout resource file with view i want to add

<TextView android:id="@+id/funnelItem" android:layout_width="387dp" android:layout_height="40dp" android:background="@color/cardview_dark_background" android:text="Funnel Cake" android:textColor="@color/cardview_light_background" android:textSize="25dp"></TextView>

Section of code within MainActivity for functionality

public class MainActivity extends AppCompatActivity implements View.OnClickListener { LinearLayout orderList; Button buttonFunnel; Button buttonOreo; Button buttonTwinkie; Button buttonSnicker;

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); orderList = findViewById(R.id.Order_List); buttonFunnel = findViewById(R.id.add_funnel_cake); buttonFunnel.setOnClickListener(this); } @Override public void onClick(View v){ addFunnelCake(); } private void addFunnelCake(){ View funnel = getLayoutInflater().inflate(R.layout.funnel_cake_row, null, false); TextView funnelCake = (TextView)funnel.findViewById(R.id.funnelItem); /* ImageView closeItem = (ImageView)funnel.findViewById(R.id.remove_item); */ orderList.addView(funnel); }

I'm sorry for the copy pasted code this is my first stack overflow post I have embedded screen shots below they are in the same order the code is copy pasted in if it makes it easier, any help is much appreciated!

First copy pasted code ScreenShot

Second copy Pasted code ScreenShot

Third copy pasted screenshot first half Third copy pasted screenshot second half

2022-05-23 16:23:56.968 19935-19935/com.example.funnel_cake_tracker E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.funnel_cake_tracker, PID: 19935 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:5106) at android.view.ViewGroup.addView(ViewGroup.java:4935) at android.view.ViewGroup.addView(ViewGroup.java:4875) at android.view.ViewGroup.addView(ViewGroup.java:4848) at com.example.funnel_cake_tracker.MainActivity.addFunnelCake(MainActivity.java:45) at com.example.funnel_cake_tracker.MainActivity.onClick(MainActivity.java:33) at android.view.View.performClick(View.java:7125) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) at android.view.View.performClickInternal(View.java:7102) at android.view.View.access$3500(View.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$Method AndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

I think you need to inflate the View you want to add differently.

View inflatedView = View.inflate(context, yourViewXML, null); //use null to not add View to parent

Solution found here: https://stackoverflow.com/a/24411058/17799032

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