繁体   English   中英

onCreate(Bundle) 已在 .com 中定义

[英]onCreate(Bundle) is already defined in .com

我如何解决这个重复的 onCreate。

我尝试将 onCreate 更改为 onCreate1

但@Override 得到一个错误说

“方法不会覆盖其超类中的方法”

我该如何解决这个问题,请帮助非常感谢

@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();
    }
    }

您不需要两个onCreate方法。 您需要在这两种方法之一中完成您需要的所有操作。

删除另一个

在 Java 中不可能有两个具有相同签名的方法。 此外,Android 会自动调用onCreate ,因此创建onCreate1方法应该在未执行的代码中解析。

消除

 @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();
    }

在您的 First 且仅onCreate添加以下行

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

例如:

@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);

        }

方法不会覆盖其超类中的方法

意味着您的覆盖方法找不到superclass方法,因为该名称没有超类方法


对于我引用这个答案

为什么我们被迫调用 super.method()?

构成 Android SDK 的类可能非常复杂。 例如,活动和片段都必须执行许多操作才能正常运行(即管理生命周期、优化内存使用、将布局绘制到屏幕等)。 要求客户端调用基类(通常在方法的开头)确保这些操作仍然被执行,同时仍然为开发人员提供合理的抽象级别。

我们怎么知道一个 函数 方法需要调用 super 呢?

文档应该告诉您是否需要这样做。 如果没有,我会在 Google 上搜索一些示例代码(或查看 API 演示……或者更好的是,查看源代码!)。 想出来应该不难。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM