0

Hi My App Keeps crashing whenever I add a scrollView to my linear layout. I need to make a dynamic buttons according to my list and call a JASON web service. This all works perfectly before I add a scroll view. How to enable scrolling to my app or add scroll view. This app will crashing. Edit Code: i tried another method and this also crashed my app

LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout);

            tcodes = new String[] {"T32","T31","T30","T39","T40","T01","T02","T03","T04","T05","T05","T07","Q01"};
    for (int i = 0; i < tcodes.length; i++) {
        LinearLayout row = (LinearLayout)findViewById(R.id.linearlayout);
        row.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        for(int j = 0; j<2;j++) {
            Button btnTag = new Button(this);
            btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            btnTag.setText(tcodes[i]);
            Drawable top = getResources().getDrawable(R.drawable.aikitting);
            btnTag.setCompoundDrawablesWithIntrinsicBounds(null,top,null,null);
            btnTag.setId(i);
            btnTag.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Button b = (Button)view;
                    String buttonText = b.getText().toString();

                    new UserNameToId().execute("http://172.16.206.195/aSSET_manager/ASSET_TABLE/hello_ikhsan?name=ikhsan&job=programmer");
                }
            });
            row.addView(btnTag);
        }
        layout.addView(row);

    }
    setContentView(layout);

this is my xml I dont add much in my XML except a constraint view

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/scrollView"
    android:textAlignment="center"
    tools:context=".MainActivity">

        <LinearLayout
            android:layout_width="match_parent"
            android:id="@+id/linearlayout"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

</ScrollView>

this is my logcat

2021-12-14 08:01:12.889 7169-7169/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 7169
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.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:3635)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
    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:2210)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7839)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
 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:5247)
    at android.view.ViewGroup.addView(ViewGroup.java:5076)
    at android.view.ViewGroup.addView(ViewGroup.java:5016)
    at android.view.ViewGroup.addView(ViewGroup.java:4988)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:697)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:201)
    at com.example.myapplication.MainActivity.onCreate(MainActivity.java:92)
    at android.app.Activity.performCreate(Activity.java:8051)
    at android.app.Activity.performCreate(Activity.java:8031)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
  • Check your crash log in android studio. It should give you a reason for the crash. – Alexander Hoffmann Dec 13 '21 at 09:09
  • share your debug log – newt Dec 13 '21 at 09:12
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Dec 13 '21 at 09:40
  • app crashes - find stack trace - research stack trace - change code - problem solved – a_local_nobody Dec 13 '21 at 09:40
  • why won't you add your ScrollView in you xml layout? Maybe it would be easier to handle. Most probably there is some NullPointerException somewhere, or IllegalStateException, but we can't really know without error logs from your app – natansalda Dec 13 '21 at 09:44

1 Answers1

1

You can not use xml recourse without setContentView, you should create a new linear layout, and you can not add to another parent if child have already.

here, your solution

    LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
                tcodes = new String[] {"T32","T31","T30","T39","T40","T01","T02","T03","T04","T05","T05","T07","Q01"};
        for (int i = 0; i < tcodes.length; i++) {
            LinearLayout row = new LinearLayout(this); //here you are making mistake
    row.setOrientation(LinearLayout.VERTICAL);
            row.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            for(int j = 0; j<2;j++) {
                Button btnTag = new Button(this);
                btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                btnTag.setText(tcodes[i]);
                Drawable top = getResources().getDrawable(R.drawable.aikitting);
                btnTag.setCompoundDrawablesWithIntrinsicBounds(null,top,null,null);
                btnTag.setId(i);
                btnTag.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Button b = (Button)view;
                        String buttonText = b.getText().toString();
                        new UserNameToId().execute("http://172.16.206.195/aSSET_manager/ASSET_TABLE/hello_ikhsan?name=ikhsan&job=programmer");
                    }
                });
                row.addView(btnTag);
            }
            layout.addView(row);
        }
        setContentView(layout);
EAS
  • 366
  • 2
  • 18