M.3bada February 2016

Error 'onCreate(Bundle) is already defined in class MainActivity' ....... Navigation Drawer And Button linked with website

In these project I use the Navigation Drawer And Button linked with website

I use Two protected void onCreate(Bundle savedInstanceState) one for the navigation and other one for Button

But codes get Error 'onCreate(Bundle) is already defined in class MainActivity'

So when I delete the second onCreate(Bundle) The code will Run but the APP stile Unfortunately, has stopped

   package com.me.test1;

import android.app.FragmentManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import com.me.test1.Fragments.MainFragment;
import com.me.test1.Fragments.a1Fragment;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer        

Answers


Cochi February 2016

You can't use two onCreate(Bundle savedInstanceState) because it is a callback function called when the activity is starting. So if there are several functions, your IDE don't know which to choose.

You should initialize most of your variables in this function and define the correcponding view for your activity with setContentView(R.layout.layout_id).

Sorry for my english.

Post Status

Asked in February 2016
Viewed 1,905 times
Voted 5
Answered 1 times

Search




Leave an answer