'不能引用在不同方法中定义的内部类中的非final变量结果'

时间:2013-07-02 10:17:56

标签: android

我是Android开发的新手,所以请耐心等待。我最近一直在关注一些对话框教程,并意识到创建一个对话框并将其显示给用户是多么冗长。所以我将所有相关的对话框代码放入一个方便的静态方法中。见下文:

public static boolean dialog(Context context, String text)
{
    boolean result = false;
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(text);
    builder.setCancelable(false);
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()  {
            public void onClick(DialogInterface dialog, int id) {
                result = true;}
            });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                result = false;}
            });
    AlertDialog dialog = builder.create();
    dialog.show();
    return result;
}

但是,当编译器不喜欢行result = trueresult = false时。它返回错误Cannot refer to a non-final variable result inside an inner class defined in a different method

我已经搜索了这个答案的解决方案,但是我得到的所有答案都没有正确理解,或者它们不能正确解决我的问题(例如,我不能只将变量设为'final')。

非常感谢任何解决此问题的建议。

5 个答案:

答案 0 :(得分:1)

Ken Wolf的回答是 NOT 解决方案。不知道,对话框(...)会在您触摸对话框按钮之前返回结果吗?它的Java基础知识。你需要在onClick上做点什么,比如调用 getActivity()。onDialogYesButton() getActivity()。onDialogNoButton()

答案 1 :(得分:0)

在您的方法之外声明result

static boolean result = false;

public static boolean dialog(Context context, String text) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(text);
    builder.setCancelable(false);
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()  {
        public void onClick(DialogInterface dialog, int id) {
            result = true;
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            result = false;
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
    return result;
}

答案 2 :(得分:0)

为了避免使用匿名委托引用的java local变量中的闭包产生的副作用,必须将其标记为final。您可以将其设为 class / static 变量。

答案 3 :(得分:0)

您可以在某处创建自己的回调界面

public interface DialogResult {
    void onDialogResult(boolean result);
}

然后将其传递给对话框

public static void dialog(Context context, String text, final DialogResult callback) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(text);
    builder.setCancelable(false);
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()  {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                callback.onDialogResult(true);
                }
            });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                callback.onDialogResult(false);
                }
            });
    AlertDialog dialog = builder.create();
    dialog.show();
}

并且对话框将来会在某个时候使用回调来通知您结果。你不能只是从未来返回一个结果。

然后像其他所有回调一样使用它

static class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ....
        Button b = (Button) findViewById(R.id.someButton);
        b.setOnClickListener(mOnClickListener);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // show dialog at some point
            dialog(v.getContext(), "Hello Dialog", mDialogCallback);
        }
    };

    private DialogResult mDialogCallback = new DialogResult() {
        @Override
        public void onDialogResult(boolean result) {
            // do something with the result
        }
    };
}

答案 4 :(得分:-1)

本地类只能访问声明为final的局部变量。