Android(w Andengine),创建一个对话框。从Seek Bar获取用户数据。将其保存到共享首选项

时间:2015-04-04 12:29:15

标签: android andengine

基本上我开发游戏并希望在用户点击/触摸两个文本(MFX和SFX卷)时创建一个对话框。我不知道它是否可以用Andengine制作,但无论如何我试图了解谷歌开发者的警报对话指南,SeekBar等后试图制作它。它如下所示。


早些时候我遇到了一个问题:

E/AndroidRuntime(1878): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

这就是为什么我让它在更新线程上运行但没有运气,他的错误仍然存​​在。我在android这个方面的工作经验很少。对不起,如果它太基础了。


private void createDialog(final int pid) {
    rM.engine.runOnUpdateThread(new Runnable() {

        @Override
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            SeekBar sb = new SeekBar(activity);
            final SharedPreferences volumePref = activity.getSharedPreferences(pid + "", Context.MODE_PRIVATE);
            final SharedPreferences.Editor editor = volumePref.edit();
            sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                int volume = volumePref.getInt(pid + "", 100);// concatenation for conversion of int to string, a short method i like

                @Override
                public void onStopTrackingTouch(SeekBar arg0) {
                    switch (pid) {
                        case MENU_MFX://These are id's which denote which volume we want to change
                        case MENU_SFX:
                            editor.putFloat(pid + "", volume);
                            editor.commit();
                            break;
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0) {
                }

                @Override
                public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                    volume = arg1;
                }
            });
            builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    rM.setMfxVolume(volumePref.getFloat(MENU_MFX + "", 1f));//These methods change volume, these work fine.
                    rM.setSfxVolume(volumePref.getFloat(MENU_SFX + "", 1f));
                }
            });
            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            });
            AlertDialog dialog = builder.create();
        }
    });


}

1 个答案:

答案 0 :(得分:0)

众所周知,有时在更新线程上运行会产生问题。所以应该这样做:

this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        MyActivity.this.showDialog(MY_DIALOG_ID);
    }
});

由@Nicolas Here告知。