简体   繁体   中英

“onCreate(Bundle savedInstanceState)” is already defined

I'm trying to make myself an app that uses webview to capture a page in one activity, but I want to make a new activity that I can get to by pressing a button, however this is stopping me :

public class MainActivity extends AppCompatActivity {
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
    }

    public void sendMessage(View view) {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        startActivity(intent);
    }
    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {

                Intent myIntent = new Intent(MainActivity.this,
                        Main2Activity.class);
                startActivity(myIntent);
            }
        });
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webView);
        myWebView.loadUrl("http://usreport.net/");
        myWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.toString());
                return true;
            }
        });
    }
}

If you can help me, I'd appreciate it. I am extremely new to all of this

You have a duplicate declaration of onCreate() . You need to combine them both to make one something like:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView myWebView = (WebView) findViewById(R.id.webView);
    myWebView.loadUrl("http://usreport.net/");
    myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.toString());
            return true;
        }
    });

    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {

            Intent myIntent = new Intent(MainActivity.this,
                    Main2Activity.class);
            startActivity(myIntent);
        }
    });
}

You need to learn to read the error and evaluate the actual issue. Things may appear complicated while you are getting started with coding, but I suggest you to go through the basics of Java before getting into Android development. Trust me it will eventually save you a lot of time.

I suggest the same Edit as mentioned by @Kamran . But i want to add a point here. Why you have to go to the same Activity whenever the back button is pressed ?

@Override
public void onBackPressed() {
    Intent intent = new Intent(MainActivity.this, MainActivity.class);
    startActivity(intent);
}

If you want to stay in the same activity all the time, Just do this

@Override
public void onBackPressed() {
    //Do nothing. AND DON'T CALL THE SUPER METHOD.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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