无法为变量赋值

时间:2014-07-22 12:48:38

标签: java android parse-platform

我是一个非常新的Android开发人员,我面临着一个奇怪的问题。我试图创建一个简单的验证测试来定义输入是否有效以及用户名不在我们中,或者相反。我尝试过各种各样的事情,例如创建一个静态的bollean' isValid'变量并分配它或使用setValid()等方法来分配它,但似乎没有任何工作。

我确信代码正在处理中(调试它)但没有任何变化,它会被误推。

请与我分享你的智慧。

public class Registration extends Fragment {
    public boolean isValid=false;

    public Registration() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View rootView = inflater.inflate(R.layout.registration, container,
                false);

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        Button bDone = (Button) getActivity().findViewById(R.id.bDone);
        bDone.setOnClickListener(new View.OnClickListener() {
            // pressed when finished entering personal details
            @Override
            public void onClick(View v) {

                final EditText fName = (EditText) getActivity().findViewById(R.id.etFirstName);
                final EditText lName = (EditText) getActivity().findViewById(R.id.etLastName);
                final EditText id = (EditText) getActivity().findViewById(R.id.etID);
                final EditText email = (EditText) getActivity().findViewById(R.id.etEmail);
                final EditText uName = (EditText) getActivity().findViewById(R.id.etRegUsername);
                final EditText pWord = (EditText) getActivity().findViewById(R.id.etRegPassword);

                ParseQuery<ParseObject> query = ParseQuery.getQuery("Account");

                String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
                String emailAddress;

                ParseObject dsAccount = new ParseObject("Account");
                dsAccount.put("firstName", fName.getText().toString());
                dsAccount.put("lastName", lName.getText().toString());
                dsAccount.put("ID", id.getText().toString());
                dsAccount.put("Email", email.getText().toString());
                dsAccount.put("username", uName.getText().toString());
                dsAccount.put("password", pWord.getText().toString());

                emailAddress = email.getText().toString();

                if (fName.getText().toString().length() == 0
                        || lName.getText().toString().length() == 0
                        || id.getText().toString().length() == 0
                        || email.getText().toString().length() == 0
                        || uName.getText().toString().length() == 0
                        || pWord.getText().toString().length() == 0) {
                    // at least one of the fields is empty
                    Toast.makeText(getActivity().getApplicationContext(),"One or more fields are missing",Toast.LENGTH_SHORT).show();
                } else if (!emailAddress.matches(emailPattern)) {
                    // email address not valid
                    Toast.makeText(getActivity().getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show();
                } else {
                    // email address valid

                    query.whereEqualTo("username", uName.getText().toString());
                    query.whereEqualTo("password", pWord.getText().toString());
                    query.getFirstInBackground(new GetCallback<ParseObject>() {
                        public void done(ParseObject Account, ParseException e) {
                            if (Account == null) {
                                isValid=true;//***this is the code that doesnt happen**
                            } else {
                                Toast.makeText(getActivity().getApplicationContext(),"Username is already in use",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                if (isValid){//**this is the code that supposed to respond**
                    dsAccount.saveInBackground();
                    Toast.makeText(getActivity().getApplicationContext(),"New account saved", Toast.LENGTH_SHORT).show();
                }
            }

        });

    }
}

2 个答案:

答案 0 :(得分:1)

您发布的代码段的问题是query.getFirstInBackground在后​​台执行,并且在调用done时检查isValid值。所以你可能想要执行

  dsAccount.saveInBackground();
  Toast.makeText(getActivity().getApplicationContext(),"New account saved", Toast.LENGTH_SHORT).show();

调用done(ParseObject Account, ParseException e) {时,而不是设置标志。此外,只有UI线程可以触摸ui

答案 1 :(得分:0)

因为您使用的是异步方法getFirstInBackground,所以不能按顺序使用代码,就像在这里尝试一样。 getFirstInBackground方法在另一个线程上触发,然后继续 - 基本上,if isValid语句检查过早发生。将这部分代码放在异步调用中,如下所示:

query.getFirstInBackground(new GetCallback<ParseObject>() {
                    public void done(ParseObject Account, ParseException e) {
                        if (Account == null) {
                            isValid=true;//***this is the code that doesnt happen**
                            dsAccount.saveInBackground();
                            makeAToast("New account saved", mActivity); // Explained down below
                        } else {
                            makeAToast("Username is already in use", mActivity);
                        }
                    }
                });

并使context成为您班级中的全局变量,例如调用Context context = this之前的onCreate()。以相同的方式创建Activity类型变量:Activity mActivity = this

因为您无法调用Toast,这是非UI线程中的UI组件(因为您要保存在后台线程中),请尝试以下操作:

public void makeAToast(String inText, Activity myActivity) {
new Thread()
{
    public void run()
    {
        myActivity.this.runOnUiThread(new Runnable()
        {
            public void run()
            {
                Toast.makeText(context,inText, Toast.LENGTH_SHORT).show();
            }
        });
    }
}.start();
}