마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

The specified child already has a parent. You must call removeView() on the child's parent first. 해결법..

0 추천

일단 제가 손전등 앱을 만들고있는데 화면으로 플래시를 쓰는 기능을 만들었는데, 거기에 메뉴버튼을 누르면 배경화면 색을 설정할수있는 메뉴가 나오고 그 메뉴버튼을 누르면 다이얼로그 창이 나오도록 만들었습니다.


문제는, 처음에 한번 누르면 나오지만 다이얼로그창을 닫고 다시 메뉴버튼을 눌러 색 설정으로 들어가면 에러가 뜨면서 앱이 팅겨버립니다. 
 
try catch 문을 이용해서 에러를 보니 
'The specified child already has a parent. You must call removeView() on the child's parent first.' 라고 나오네요. 이미 자식이 부모가 있으니 removeView()메소드를 이용해서 제거하라는거 같은데..
 
어떻게 해결하죠?
혹시 모르니 동영상 첨부할게요.
 
코드 올려드립니다.
 
Screen_Light.java
package com.example.flashlight;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class ScreenLight extends Activity {

    LinearLayout mFlash_screen;
    WindowManager.LayoutParams mScreen;
    ColorOption_Dialog option_dialog;
    int r = 255;
    int g = 255;
    int b = 255;


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

        mFlash_screen = (LinearLayout) findViewById(R.id.flash_screen);
        mScreen = getWindow().getAttributes();
        mScreen.screenBrightness = 1f;
        getWindow().setAttributes(mScreen);


        mFlash_screen.setBackgroundColor(Color.rgb(r, g, b)); //배경색을 초기화 한다.
        option_dialog = new ColorOption_Dialog(this);

    }

    @Override
    public boolean onCreateOptionsMenu (Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.screenlight, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
        switch (item.getItemId()) {
            case R.id.coloroption:
                try {
                    option_dialog.show();
                } catch (RuntimeException e) {
                    Log.e("에러", e.getMessage().toString()); //이 부분에서 에러가 남
                }

                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onStart () {
        super.onStart();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    protected void onPause () {
        super.onPause();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    protected void onDestroy () {
        super.onDestroy();

        mScreen.screenBrightness = 0f; // (중요!)이게 없으면 폰에 버그가 생긴다.
        getWindow().setAttributes(mScreen);
    }
}

 

 

ColorOption_Dialog.java

package com.example.flashlight;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;

public class ColorOption_Dialog extends AlertDialog.Builder {

    Context mContext;

    public ColorOption_Dialog (Context context) {
        super(context);
        this.mContext = context;

        View CO_dialog = View.inflate(mContext, R.layout.coloroption_dialog, null);
        setView(CO_dialog).setTitle("배경색 설정");
    }
}

 

 

익명사용자 님이 2015년 6월 13일 질문

1개의 답변

0 추천
안녕하세요.

 Back키 눌렀을 경우 Dialog Cancle 여부 설정이 되어있는지 확인 해보셔요~

그리고 더불어~ option_dialog를 dismiss해주는 부분은 존재하나요 ??(option_dialog.dismiss();)

없다면 아래의 주소의 블로그를 참조하셔서 dialog 여러가지 설정도 해보셔요~~

(예를들면 dialog밖을 눌렀을때 사라지는 것 등등..)

http://arabiannight.tistory.com/360
dubudub (930 포인트) 님이 2015년 6월 14일 답변
dubudub님이 2015년 6월 14일 수정
...