코드는 다음과 같습니다.
코드가 굉장히 길어보이지만,
그냥 쓰레드가 실행되는 도중에 다이얼로그를 띄운 다음
레이아웃과 라디오 버튼, 체크박스 등의 각종 뷰들을
다이얼 로그에 추가하기 위해 runOnUiThread를 실행한 코드입니다.
runOnUiThread(new Runnable()
{
@Override
public void run()
{
LinearLayout dialogLayout = new LinearLayout(mContext);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
dialogLayout.setGravity(Gravity.CENTER);
// 세로 정렬에 가운데 정렬인 리니어 레이아웃 선언
LinearLayout subLayout = new LinearLayout(mContext);
subLayout.setOrientation(LinearLayout.HORIZONTAL);
subLayout.setGravity(Gravity.CENTER);
// 가로 정렬에 가운데 정렬인 서브 레이아웃 선언
CheckBox music = new CheckBox(mContext);
music.setChecked(mMusic);
music.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
mMusic = isChecked;
mEditor.putBoolean(Key.MUSIC, isChecked).commit();
}
});
music.setText(getString(R.string.Music));
music.setTextSize(20);
music.setGravity(Gravity.CENTER);
CheckBox soundEffect = new CheckBox(mContext);
soundEffect.setChecked(mSP.getBoolean(Key.SOUND_EFFECT, true));
soundEffect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
mSoundEffect = isChecked;
mEditor.putBoolean(Key.SOUND_EFFECT, isChecked).commit();
}
});
soundEffect.setText(getString(R.string.SoundEffect));
soundEffect.setTextSize(20);
soundEffect.setGravity(Gravity.CENTER);
subLayout.addView(music);
subLayout.addView(soundEffect);
RadioGroup rg = new RadioGroup(mContext);
final RadioButton rb1 = new RadioButton(mContext);
final RadioButton rb2 = new RadioButton(mContext);
final RadioButton rb3 = new RadioButton(mContext);
rg.addView(rb1);
rg.addView(rb2);
rg.addView(rb3);
rg.setGravity(Gravity.CENTER);
rb1.setChecked(true);
rb1.setText(getString(R.string.GameResume));
rb1.setTextSize(20);
rb2.setText(getString(R.string.Restart));
rb2.setTextSize(20);
rb3.setText(getString(R.string.GoToMain));
rb3.setTextSize(20);
dialogLayout.addView(subLayout);
dialogLayout.addView(rg);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setView(dialogLayout);
alertDialogBuilder.setTitle(getString(R.string.GamePauseTitle));
alertDialogBuilder.setMessage(getString(R.string.GamePauseMessage));
alertDialogBuilder.setCancelable(true).setPositiveButton(getString(R.string.OK), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if(rb1.isChecked())
{
Toast.makeText(mContext, getString(R.string.GameResumeToast), Toast.LENGTH_SHORT).show();
if(mGameView != null)
mGameView.gameStart();
}
else if(rb2.isChecked())
{
Toast.makeText(mContext, getString(R.string.RestartToast), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(BossStage1.this, BossStage1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
overridePendingTransition(0, 0);
}
else if(rb3.isChecked())
{
mSound.release();
mSound = null;
Toast.makeText(mContext, getString(R.string.GoToMainToast), Toast.LENGTH_SHORT).show();
BossStage1.super.onBackPressed();
overridePendingTransition(0, 0);
}
}
});
alertDialogBuilder.setOnCancelListener(new DialogInterface.OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog)
{
if(mGameView != null)
mGameView.gameStart();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// 다이얼로그 생성
alertDialog.setCanceledOnTouchOutside(false);
// 알림창이 띄워진 상태에서 알림창 바깥쪽을 터치했을 때 알림창을 닫을것이냐를 설정하는 코드
alertDialog.show();
// 다이얼로그 보여주기
}
});
참고로 위 코드는 뒤로가기를 눌렀을 때 (즉, onBackPressed() 함수가 실행될 때)
그리고 게임 오버가 되었을 때
이 두 가지 상황에서 실행됩니다.
근데 아래와 같은 오류가 계속 반복적으로 뜨더군요.
그것도 엄청 빠르게 ...
at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7052)
at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7052)
at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:7052)
...
...
...
원인과 해결법이 무엇인지 궁금합니다.
인터넷에 검색해봐도 자료가 안나와서...
도와주세요 ㅠㅠ