-1

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$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

1 Answers1

0

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

Daniel Knauf
  • 559
  • 3
  • 11
  • what should i be using as the context here I've tried null, orderList.getContext(), this.getApplicationContext(), and getApplicationCOntext() all of these result in the program crashing Here is a copy paste of how i have the code currently Context context = getApplicationContext(); View funnel = View.inflate(context, R.layout.funnel_cake_row, orderList); – Dean Lisko May 23 '22 at 21:13
  • As your code is inside an `Activity`, you can just pass itself as the context: View.inflate(this, R.layout.funnel_cake_row, orderList); – Daniel Knauf May 23 '22 at 21:19
  • I also tried that and it still leads to crashing. So it must be somewhere else thats causing it. Thank you for the assisstance i really appreciate it! – Dean Lisko May 23 '22 at 22:24
  • If you add the stack trace of the crash to your post we can have a look at it. Or maybe its a new question, I don't know :D – Daniel Knauf May 23 '22 at 22:39
  • I believe i added the stack trace to my post! – Dean Lisko May 23 '22 at 23:27
  • Thanks. I see the problem: `View.inflate(context, yourViewXML, yourLinearLayout);` . Here I already added the `View` to a parent...I think: ` View.inflate(context, yourViewXML, null);` should fix it. I also updated my answer. – Daniel Knauf May 24 '22 at 06:39
  • Yes it is now working thank you so much I really appreciate the help! I would have fixed it sooner but i didnt realize i had to hit show more comments lol. – Dean Lisko May 25 '22 at 05:23