简体   繁体   English

已经定义“ onCreate(捆绑保存的InstanceState)”

[英]“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 : 我正在尝试使自己成为一个使用webview捕获一个活动中的页面的应用程序,但是我想创建一个新活动,我可以通过按一个按钮来完成该活动,但这阻止了我:

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() . 您有重复的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. 在开始进行编码时,事情可能看起来很复杂,但是我建议您在进入Android开发之前先了解一下Java的基础知识。 Trust me it will eventually save you a lot of time. 相信我,它将最终为您节省很多时间。

I suggest the same Edit as mentioned by @Kamran . 我建议使用@Kamran提到的相同Edit。 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.
}

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

相关问题 错误:方法 onCreate(Bundle) 已在类 MainActivity 中定义 public void onCreate(Bundle savedInstanceState) { - Error: method onCreate(Bundle) is already defined in class MainActivity public void onCreate(Bundle savedInstanceState) { 捆绑的saveInstanceState在onCreate()上为null,尽管saveInstanceState已经设置了数据 - Bundle savedInstanceState is null on onCreate() although savedInstanceState is already set data onCreate(Bundle) 已在 .com 中定义 - onCreate(Bundle) is already defined in .com 'onCreate(Bundle)' 已在活动中定义 - 'onCreate(Bundle)' is already defined in activity 什么是 onCreate(Bundle savedInstanceState) - What's onCreate(Bundle savedInstanceState) FragmentActivity在onCreate savedInstanceState Bundle中的NullPointer - FragmentActivity NullPointer in onCreate savedInstanceState Bundle onCreate(Bundle savedInstanceState)始终为null - onCreate(Bundle savedInstanceState) in always null 方法 onCreate(Bundle) 已经在类 MainActivity 中定义 - Method onCreate(Bundle) is already defined in class MainActivity 错误:此活动中已经定义了onCreate(Bundle) - Error : onCreate(Bundle) is already defined in this activity 在onCreate(Bundle savedInstanceState)中创建捆绑对象的位置在哪里 - Where is Bundle object created in onCreate(Bundle savedInstanceState)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM