简体   繁体   中英

App crashes while trying popping up a dialog

I have a button in my activity layout. When the button is clicked a dialog pops up. When I tap outside the bounds of the dialog the dialog disappears. (I do not have any problem with this issue).

But when I click on the button again I expect the dialog to pop up again but the app crashes instead.

I am just playing around with dialogs to actually implement it in my real app

My stacktrace:

2020-08-01 12:00:33.377 9333-9333/com.example.spinner E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.spinner, PID: 9333
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:4941)
    at android.view.ViewGroup.addView(ViewGroup.java:4772)
    at android.view.ViewGroup.addView(ViewGroup.java:4744)
    at androidx.appcompat.app.AlertController.setupCustomContent(AlertController.java:657)
    at androidx.appcompat.app.AlertController.setupView(AlertController.java:475)
    at androidx.appcompat.app.AlertController.installContent(AlertController.java:233)
    at androidx.appcompat.app.AlertDialog.onCreate(AlertDialog.java:279)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:403)
    at android.app.Dialog.show(Dialog.java:302)
    at androidx.appcompat.app.AlertDialog$Builder.show(AlertDialog.java:1009)
    at com.example.spinner.MainActivity$1.onClick(MainActivity.java:40)
    at android.view.View.performClick(View.java:6304)
    at android.view.View$PerformClick.run(View.java:24803)
    at android.os.Handler.handleCallback(Handler.java:794)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:6651)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

My activity code:

public class MainActivity extends AppCompatActivity {
    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<String> avc = new ArrayList<String>();
        avc.add("sb");
        avc.add("eerig");
        avc.add("sb");
        avc.add("eerig");
        avc.add("sb");
        avc.add("eerig");
        avc.add("sb");
        avc.add("eerig");
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, avc);
        spinner = new Spinner(this);
        spinner.setAdapter(adapter);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("abc");
                builder.setMessage("abcde");
                builder.setView(spinner);
                builder.show();
            }
        });
    }
}

I have the following code at line 40 of my code: builder.show();

I want to know why am I getting this error and also how to solve it.

Picture of the dialog (and layout): 在此处输入图像描述

This issue is happening because of that spinner which is already attached to the previous dialog or in another word has parent. You should create it each time you want to show your dialog:

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("abc");
                builder.setMessage("abcde");
                ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, avc);
                spinner = new Spinner(this);
                spinner.setAdapter(adapter);
                builder.setView(spinner);
                builder.show();
            }
        });

adjust your code.

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            mSpinner = new Spinner(this);
            mSpinner.setAdapter(adapter);
    
            AlertDialog.Builder builder = new 
            AlertDialog.Builder(MainActivity.this);
            builder.setTitle("abc");
            builder.setMessage("abcde");
            builder.setView(mSpinner);
            builder.show();
            }
       });

By the way, this is terrible way to do this.

Try this.

declare your ArrayAdapter just below the spinner Declaration:

public class MainActivity extends AppCompatActivity {
private Spinner spinner;
private ArrayAdapter adapter;

and then initialize it in the onCreate:

adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, avc);

the lastly move this code to the onClick (inside onClick):

spinner = new Spinner(this);
spinner.setAdapter(adapter);

like this:

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("abc");
            builder.setMessage("abcde");
            spinner = new Spinner(this);
            spinner.setAdapter(adapter);
            builder.setView(spinner);
            builder.show();
        }
    });

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