4

すぐに助けが必要です!! コードは正常に動作していましたが、しばらくすると、評価がt work anymore and gives "the specified child already has a parent, call removeView() first..." on thealertDialog.show();` 行にないことに気付きました。

///// Rating bar initialising
    Context mContext = ShowActivity.this;
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    layout = inflater.inflate(R.layout.custom_rate_dialog, (ViewGroup) findViewById(R.id.layout_root));

    final RatingBar rating_indicator = (RatingBar)findViewById(R.id.rating_bar_cafe_indicator);
    final float current_rating = (float)mDbHelper.getInt(mRowId, type, RATE_COLUMN);
    rating_indicator.setRating(current_rating);


    rateDialogBuilder = new AlertDialog.Builder(ShowActivity.this);
    rateDialogBuilder.setView(layout);
    rateDialogBuilder.setCancelable(false);
    rateDialogBuilder.setIcon(R.drawable.rate_star_med_on);
    rateDialogBuilder.setTitle("RATE IT!");
    rateDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            rating_indicator.setRating(mDbHelper.getInt(mRowId, type, RATE_COLUMN));
            ((ViewGroup)layout.getParent()).removeView(layout);
            dialog.cancel();
        }
    });

    ratingText = (TextView) layout.findViewById(R.id.rate_text);
    rating = (RatingBar) layout.findViewById(R.id.rating_bar_cafe);
    rating.setRating(current_rating);
    rating.setStepSize(1);
    rating.setOnRatingBarChangeListener(ShowActivity.this);

    if ((int)current_rating != 0) ratingText.setText("Your rating: " + (int)current_rating); 

    Button rateButton = (Button) findViewById(R.id.button_rate); 
    rateButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            AlertDialog alertDialog = rateDialogBuilder.create(); 
            alertDialog.show();

        }
    });

    //////

奇妙なことに、電話をかけた場合、removeAllViews()またはクラッシュするremoveView()直前にalertDialog.show();、評価と現在のアクティビティまたはスムスの両方を確認できるめちゃくちゃなダイアログが開きます。また、これは以前は正常に機能していたため、問題はコードの別の場所にある可能性があります。何を探すべきか、またはこれを機能させるためにこれを変更する方法はありますか?

4

1 に答える 1

11

あなたが得ているエラーは、Viewすでに親に追加された後に親に追加しようとしているということです。

コードを見ると、次のようになります。

layout = inflater.inflate(R.layout.custom_rate_dialog, (ViewGroup) findViewById(R.id.layout_root));

あなたに追加custom_rate_dialogしますlayout_root。次に、それを別の親に追加しますrateDialogBuilder.setView(layout);

そのレイアウトをインフレートして として設定しようとしているだけの場合はViewDialog

layout = inflater.inflate(R.layout.custom_rate_dialog, null);
于 2011-12-14T02:11:57.477 に答える