无法解析符号'v'和期望)和;

时间:2017-01-23 08:21:50

标签: java android lambda

请帮助解决此错误的方法。我似乎无法得到它。

我在这部分中遇到了错误。

 signup.setOnClickListener((v) → {
        String uName = username.getText().toString().trim();
        String mail = email.getText().toString().trim();
        String pass = password.getText().toString().trim();

        signup(uName, mail, pass);

        });

整个代码:

public class SignUp extends AppCompatActivity {
    private String TAG = SignUp.class.getSimpleName();
    private EditText username, email, password;
    private Button signup;
    private ProgressDialog progressDialog;
    private UserSession session;
    private UserInfo userInfo;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        username = (EditText) findViewById(R.id.username);
        email = (EditText) findViewById(R.id.email);
        password = (EditText) findViewById(R.id.password);
        signup = (Button) findViewById(R.id.signup);
        progressDialog = new ProgressDialog(this);

        GoogleApiClient client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

        session = new UserSession(this);
        userInfo = new UserInfo(this);


        signup.setOnClickListener((v) → {
        String uName = username.getText().toString().trim();
        String mail = email.getText().toString().trim();
        String pass = password.getText().toString().trim();

        signup(uName, mail, pass);

        });

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client2 = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    private void signup(final String username, final String email, final String password) {
        //Tag used to cancel the request
        String tag_string_req = "req_signup";
        progressDialog.setMessage("Signing up...");
        progressDialog.show();

        StringRequest strReq = new StringRequest(Request.Method.POST,
                Utils.REGISTER_URL, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Register Response: " + response.toString());

                try {
                    JSONObject jOBj = new JSONObject(response);
                    boolean error = jOBj.getBoolean("error");

                    //Check for error node in json
                    if (!error) {
                        JSONObject user = jOBj.getJSONObject("user");
                        String uName = user.getString("username");
                        String email = user.getString("email");

                        //Inserting row in users table
                        userInfo.setEmail(email);
                        userInfo.setUsername(uName);
                        session.setLoggedin(true);

                        startActivity(new Intent(SignUp.this, MainActivity.class));

                    } else {
                        //Error in login. Get the error message
                        String errorMsg = jOBj.getString("error_msg");
                        toast(errorMsg);
                    }
                } catch (JSONException e) {
                    //JSON error
                    e.printStackTrace();
                    toast("Json error: " + e.getMessage());
                }

            }


        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Login Error: " + error.getMessage());
                toast("Unknown Error occured");
                progressDialog.hide();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                //posting parameters to login url
                Map<String, String> params = new HashMap<>();
                params.put("username", username);
                params.put("email", email);
                params.put("password", password);

                return params;
            }

        };
    }

    private void toast(String x) {
        Toast.makeText(this, x, Toast.LENGTH_SHORT)
    }


    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("SignUp Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client2.connect();
        AppIndex.AppIndexApi.start(client2, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client2, getIndexApiAction());
        client2.disconnect();
    }
}

请解释发生了什么。

3 个答案:

答案 0 :(得分:3)

lambda语法使用->,而不是。你是从哪里得到的? (编辑:似乎这是IntelliJ的代码缩短。你是否从IntelliJ复制了这个?)

如果你想使用lambdas,请使用它。

signup.setOnClickListener(v -> {
    String uName = username.getText().toString().trim();
    String mail = email.getText().toString().trim();
    String pass = password.getText().toString().trim();
    signup(uName, mail, pass);
});

这只适用于Java 8(在Gradle文件中设置您的版本等) 请参阅Use Java 8 Language Features

编辑:Java 7及更低版本不支持lambdas。如果你想使用Java&lt; 8,使用完整的界面和匿名的内部类:

signup.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String uName = username.getText().toString().trim();
        String mail = email.getText().toString().trim();
        String pass = password.getText().toString().trim();
        signup(uName, mail, pass);
    }
});

修改:为什么要使用getText().toString()?无论如何getText还没有String吗?您可以在代码中删除对toString的这些来电。

答案 1 :(得分:0)

您应该像下面的代码一样设置setOnClickListener。 用

替换你的代码
signup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String uName = username.getText().toString().trim();
                    String mail = email.getText().toString().trim();
                    String pass = password.getText().toString().trim();
                    signup(uName, mail, pass);
                }
            });

答案 2 :(得分:0)

用以下内容替换你的onclick监听器:

signup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           String uName = username.getText().toString().trim();
           String mail = email.getText().toString().trim();
           String pass = password.getText().toString().trim();

            signup(uName, mail, pass);
        }
    });