일단 제가 손전등 앱을 만들고있는데 화면으로 플래시를 쓰는 기능을 만들었는데, 거기에 메뉴버튼을 누르면 배경화면 색을 설정할수있는 메뉴가 나오고 그 메뉴버튼을 누르면 다이얼로그 창이 나오도록 만들었습니다.
문제는, 처음에 한번 누르면 나오지만 다이얼로그창을 닫고 다시 메뉴버튼을 눌러 색 설정으로 들어가면 에러가 뜨면서 앱이 팅겨버립니다.
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("배경색 설정");
}
}