Home > Net >  'onCreate(Bundle)' is already defined in 'com.krish.galaxypdfviewer.MainActivity
'onCreate(Bundle)' is already defined in 'com.krish.galaxypdfviewer.MainActivity

Time:07-29

I added a button and tried to link it with my 2nd activity name 'website' but its saying onCreate(Bundle)' is already defined i dont know how to fix screenshot of error

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        defineView();
        handleIntent();
        defineActionBar();
        checkPermission();
    }



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

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openWebsite();
            }
        });
    }

    public void openWebsite() {
        Intent intent = new Intent(this, Website.class);
        startActivity(intent);
    }

CodePudding user response:

You have defined onCreate() Method for two times, you need to remove one of them.

what you can do is cut this code from the second onCreate() and paste it to the first one.

button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openWebsite();
            }
        });

So your final onCreate() code will be like this :

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

        button = (Button) findViewById(R.id.button);
        
        defineView();
        handleIntent();
        defineActionBar();
        checkPermission();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openWebsite();
            }
        });
    }



    public void openWebsite() {
        Intent intent = new Intent(this, Website.class);
        startActivity(intent);
    }
  • Related