开发者

Problem with Cursor

开发者 https://www.devze.com 2023-02-01 01:09 出处:网络
I am trying to pass a Cursor to a method. But i am getting this error. Cannot refer to a non-final variable c inside an inner class defined in a different method

I am trying to pass a Cursor to a method. But i am getting this error.

Cannot refer to a non-final variable c inside an inner class defined in a different method

the code.

public void loggedin(String title, String message, String positive, Cursor c) {
    AlertDialog.Builder alertbox = new AlertDialog.Builder(
            HomeActivity.this);
    alertbox.setTitle(title);
    alertbox.setMessage(message);
    alertbox.setPositiveButton(positive,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    String username = c.getString(0);
                    MessagingApplication.setUsername("");
                    String currentUserName=MessagingApplication.getUsername();
                    Toast.makeText(HomeActivity.this, currentUserName, 5);

                    alertbox("Error", currentUserName,
                            "Back", null);

                    Intent i = new Intent(HomeActivity.this,
                            MailActivity.class);
                    //startActivity(开发者_如何学运维i);
                }
            });

    alertbox.show();
}


Your OnClickListener is an anonymous inner class. You can't use non-final variables with scope outside of the anonymous inner class.

This is because an anonymous inner class gets a copy of all local variables so they will be available when the inner class is used. If the variable wasn't marked final, there is no guarantee that the value would be the same.

So you need to declare your cursor as final:

public void loggedin(String title, String message, String positive, final Cursor c) {
0

精彩评论

暂无评论...
验证码 换一张
取 消