您必须先在子父级上调用removeview。 Android的

时间:2014-11-21 15:56:56

标签: android spinner alertdialog

我遇到了打开微调器的问题。当我第一次打开它时它正确打开并允许我从中进行选择。然后,当我第二次尝试打开它时,它给了我一个错误:

11-21 18:43:49.589: E/AndroidRuntime(3178): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我的微调器点击ImageButton打开。代码如下:

    final ArrayAdapter<String> adp = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, s);
    final Spinner sp = new Spinner(this);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    ImageButton bracket = (ImageButton) findViewById(R.id.imageButton1);
    bracket.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    choosed_bracket = (String) sp.getItemAtPosition(i);
                }

                public void onNothingSelected(AdapterView<?> adapterView) {
                    return;
                }
            });
            sp.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            sp.setAdapter(adp);

            builder.setTitle("Choose war bracket:");
            builder.setView(sp);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    /*SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("bracket", choosed_bracket);
                    editor.commit(); */
                    Toast.makeText(getApplicationContext(), "Choosed bracket is: " + choosed_bracket, Toast.LENGTH_LONG).show();
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.create().show();
        }
    });

希望你能帮助我!

1 个答案:

答案 0 :(得分:1)

您是否尝试在onclick事件中实例化您的构建器而不是在外面?

 ImageButton bracket = (ImageButton) findViewById(R.id.imageButton1);
    bracket.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            ArrayAdapter<String> adp = new ArrayAdapter<String>(arg0.getContext(), android.R.layout.simple_spinner_item, s);
            Spinner sp = new Spinner(arg0.getContext());
            AlertDialog.Builder builder = new AlertDialog.Builder(arg0.getContext());
            sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    choosed_bracket = (String) sp.getItemAtPosition(i);
                }

                public void onNothingSelected(AdapterView<?> adapterView) {
                    return;
                }
            });
            sp.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            sp.setAdapter(adp);

            builder.setTitle("Choose war bracket:");
            builder.setView(sp);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    /*SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("bracket", choosed_bracket);
                    editor.commit(); */
                    Toast.makeText(getApplicationContext(), "Choosed bracket is: " + choosed_bracket, Toast.LENGTH_LONG).show();
                }
            });