-1

How do I fix this duplication of onCreate.

I tried changing onCreate to onCreate1

but @Override gets an error saying

"Method does not override method from its superclass"

how do i fix this pls help thank you so much

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

            email = (EditText) findViewById(R.id.email);
            password = (EditText) findViewById(R.id.password);
            login = (Button) findViewById(R.id.login);
            signup = (TextView) findViewById(R.id.open_signup);
            progressDialog = new ProgressDialog(this);
            session = new UserSession(this);
            userInfo = new UserInfo(this);

            if (session.isUserLoggedin()) {
                startActivity(new Intent(this, MainActivity.class));
                finish();

            }

            login.setOnClickListener(this);
            signup.setOnClickListener(this);

        }

        private void login(final String email, final String password) {
            //Tag used to cancel the request
            String tag_string_req = "req_login";
            progressDialog.setMessage("Logging in....");
            progressDialog.show();

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

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

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

                    //check for error node in json
                    if (!error) {
                        //now store the user in SQLite
                        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(Login.this, MainActivity.class));
                        finish();
                    } 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("email", email);
                params.put("password", password);

                return params;

            }

            ;

//Adding request to request queue
            AndroidLoginController.getInstance();

            addToRequestQueue(strReq, tag_string_req);
        }

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

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                String uName = email.getText().toString().trim();
                String pass = password.getText().toString().trim();

                login(uName, pass);
                break;
            case R.id.open_signup:
                startActivity(new Intent(this, SignUp.class));
                break;

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

    /**
     * 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("Login 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.
        client.connect();
        AppIndex.AppIndexApi.start(client, 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(client, getIndexApiAction());
        client.disconnect();
    }
    }
Pauline R
  • 1
  • 2

2 Answers2

0

You don't need two onCreate methods. You need to do all what you need into one of these two methods.

Delete the other one

It's not possible to have two methods with the same signature in Java. Furthermore onCreate is called automatically by Android, so creating a onCreate1 method should resolve in non executed code.

firegloves
  • 5,581
  • 2
  • 29
  • 50
0

Remove

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

add below line in your First and only onCreate

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

eg:

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

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


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

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

            if (session.isUserLoggedin()) {
                startActivity(new Intent(this, MainActivity.class));
                finish();

            }

            login.setOnClickListener(this);
            signup.setOnClickListener(this);

        }

Method does not override method from its superclass

means your override method can't find the superclassmethod, because there is no superclass method by that name


For me quoting this answer

Why are we forced to call super.method()?

The classes that make up the Android SDK can be incredibly complex. For instance, both activities and fragments must perform a number of operations in order to function properly (i.e. managing life cycle, optimizing memory usage, drawing the layout to the screen, etc.). Requiring the client to make a call to the base class (often at the beginning of the method) ensures that these operations are still performed, while still providing a reasonable level of abstraction for the developer.

How do we know that a function method requires super to be called?

The documentation should tell you whether or not this is required. If it doesn't I'd Google-search for some sample code (or check the API demos... or better yet, look at the source code!). It shouldn't be too difficult to figure out.

Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • @Pauline R did you fixed your issue? – Charuක Jan 21 '17 at 15:12
  • Nice explanation.. But it should be `overriden method cannot find the superclass function` and not just `superclass`. The superclass is the `Activity` class which exists but doesn't have any function called `onCreate2` so override throws an error – ashkhn Jan 21 '17 at 17:09
  • 1
    No I meant the next line i.e `means your override method can't find the superclass, because there is no superclass by that name` should be changed – ashkhn Jan 21 '17 at 17:20
  • @akash93 bah gotccha ill update that.Thanks for pointing out! – Charuක Jan 21 '17 at 17:22
  • @Pauline R Click on the error it will give you the option or GLOBALLY use `GoogleApiClient client;` – Charuක Jan 23 '17 at 02:06