0

This error comes at the time of click the create one and go to the signup

    Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                                          at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:204)
                                                                                          at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
                                                                                          at com.sixtinbyte.codeexpertise.MainActivity.onCreate(MainActivity.java:49)

Error when clicking on the login button but I don't know how to use HomeFragment.class (fragment) in android manifest.xml

    FATAL EXCEPTION: main
                                                                                      Process: com.sixtinbyte.codeexpertise, PID: 18560
                                                                                      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sixtinbyte.codeexpertise/com.sixtinbyte.codeexpertise.HomeFragment}: java.lang.ClassCastException: com.sixtinbyte.codeexpertise.HomeFragment cannot be cast to android.app.Activity

I take the navigation drawer in this navigation drawer I have 2 activities loginactivity and mainactivity. when I click on the login button then its go to the HomeFragement but it's showing the error and when I click on the create one then its go to the content_main (In this content_main I have my signup page). But this also shows the error.

  content_main.xml 

       <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context=".MainActivity"
            tools:showIn="@layout/app_bar_main"
            android:background="@android:color/transparent">

            <FrameLayout
                android:id="@+id/fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

                <RelativeLayout
                    android:id="@+id/openDialog"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:orientation="horizontal"
                    android:background="@android:color/holo_red_light"
                    android:layout_gravity="right|top"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginBottom="112dp">

                    <TextView
                        android:id="@+id/text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="#555555"
                        android:singleLine="true"
                        android:textSize="18dp"
                        android:layout_centerVertical="true"
                        android:layout_centerHorizontal="true"
                        android:text="Experiences" />

                </RelativeLayout>

            <EditText
                android:id= "@+id/firstname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="First Name"/>

            <EditText
                android:id= "@+id/lastname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Last Name"
                android:layout_below="@+id/firstname"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="32dp" />

            <EditText
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Email"
                android:layout_below="@+id/lastname"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="41dp" />

            <AutoCompleteTextView
                android:id="@+id/autoCompleteTextView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Location"
                android:ems="10"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />


            <Button
                android:id="@+id/signup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sign up"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="34dp" />


        </RelativeLayout>  




MainActivity.xml



  import android.content.Intent;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.NavigationView;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.FragmentManager;
    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.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;


    public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener, AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {

        public EditText firstname, lastname, email;
        //private Toolbar toolbar;
        AutoCompleteTextView textView=null;
        public String emailRegistration;
        private ArrayAdapter<String> adapter;
        public Button signup;
        String item[]={
                "Indore", "Ujjain", "Bhopal", "Pune",
                "Hyderabad", "Chennai", "Mumbai", "Bangalore",
                "Rajkot", "Delhi", "Chandigarh", "Tamil Nadu"
        };

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

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            //toolbar.setTitle("toolbar");
            setSupportActionBar(toolbar);
            textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, item);
            textView.setThreshold(1);
            //Set adapter to AutoCompleteTextView
            textView.setAdapter(adapter);
            textView.setOnItemSelectedListener(this);
            textView.setOnItemClickListener(this);
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
             firstname = (EditText) findViewById(R.id.firstname);
            if( firstname.getText().toString().length() == 0 )
                firstname.setError("First name is required!" );
            lastname = (EditText)findViewById(R.id.lastname);
            if( lastname.getText().toString().length() == 0 )
                lastname.setError("Last name is required!" );
            email = (EditText)findViewById(R.id.email);
    //        if (!emailRegistration.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+")) {
    //
    //            email.setError("Invalid Email Address");
    //
    //        }
            signup = (Button)findViewById(R.id.signup);
            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();
                }
            });
            signup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(MainActivity.this,HomeFragment.class);
                    startActivity(intent);
                }
            });

                    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }

        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();

            if (id == R.id.nav_camera) {
            setTitle("Home");
                HomeFragment homeFragment = new HomeFragment();
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.fragment,homeFragment).commit();
            } else if (id == R.id.nav_gallery) {
                setTitle("Edit Profile");
                EditFragment editFragment = new EditFragment();
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.fragment,editFragment).commit();

            } else if (id == R.id.nav_slideshow) {
                setTitle("Business");
                BusinessFragment businessFragment = new BusinessFragment();
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.fragment,businessFragment).commit();

            } else if (id == R.id.nav_manage) {
                setTitle("Message");
                MessageFragment messageFragment = new MessageFragment();
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.fragment,messageFragment).commit();

            } else if (id == R.id.nav_share) {

            } else if (id == R.id.nav_send) {

            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            // TODO Auto-generated method stub
            //Log.d("AutocompleteContacts", "onItemSelected() position " + position);

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            InputMethodManager imm = (InputMethodManager) getSystemService(
                    INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // TODO Auto-generated method stub

            // Show Alert
            Toast.makeText(getBaseContext(), "Position:"+i+" Country:"+adapterView.getItemAtPosition(i),
                    Toast.LENGTH_LONG).show();

            Log.d("AutocompleteContacts", "Position:"+i+" Country:"+adapterView.getItemAtPosition(i));

        }
    }  



login.xml 

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_orange_dark">
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/we_are_here_to_generate_more_business_for_you"
            android:textColor="@android:color/white"
            android:textSize="18sp"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/email"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="79dp"
            android:id="@+id/email"
            android:inputType="none" />
        <EditText
            android:id="@+id/text_input_password_toggle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:password="true"
            android:hint="@string/password"
            android:layout_below="@+id/email"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="45dp" />

        <CheckBox
            android:id="@+id/ch_rememberme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/remember_password"
            android:layout_below="@+id/text_input_password_toggle"
            android:layout_alignLeft="@+id/code"
            android:layout_alignStart="@+id/code" />

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/login"
            android:layout_below="@+id/ch_rememberme"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="19sp"
            android:text="@string/not_a_member_create_one"
            android:layout_below="@+id/login"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="38dp" />

        <TextView
            android:id="@+id/forgotpassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="19sp"
            android:text="@string/forgot_password"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_below="@+id/text2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="49dp" />

        <TextView
            android:id="@+id/code"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/powered_by_code_expertise"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />





    </RelativeLayout> 

 LoginActivity.xml



 import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;


    public class LoginActivity extends AppCompatActivity {
        public EditText email, password;
        public TextView textView;
        public Button login;


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

            email = (EditText) findViewById(R.id.email);
    //        if( email.getText().toString().length() == 0 )
    //            email.setError("Email is required!" );
            password = (EditText) findViewById(R.id.text_input_password_toggle);
    //        if( password.getText().toString().length() == 0 )
    //            password.setError("Password is required!" );
            textView = (TextView) findViewById(R.id.text2);
            login = (Button) findViewById(R.id.login);

            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(LoginActivity.this, HomeFragment.class);
                    startActivity(intent);
                }
            });

            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(i);
                }
            });
        }



        }

AndroidManifest.xml



     <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.sixtinbyte.codeexpertise">

            <uses-permission android:name="android.permission.INTERNET" />

            <application
                android:allowBackup="true"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"
                android:supportsRtl="true"
                android:theme="@style/AppTheme">
                <activity
                    android:name=".LoginActivity"
                    android:label="@string/app_name"
                    android:theme="@style/AppTheme.NoActionBar">
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
                <activity android:name=".MainActivity"></activity>
                <activity android:name=".HomeFragment"></activity>



            </application>

        </manifest>
Ronak Thakkar
  • 2,515
  • 6
  • 31
  • 45

2 Answers2

0

you added theme with @style/AppTheme.NoActionBar it's made problem change it with @style/AppTheme

 <activity
                android:name=".LoginActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57
  • Hey Shweta its showing the error like this :- Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. –  Jul 12 '17 at 07:28
  • Its still showing the error but this error is different now :- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference –  Jul 12 '17 at 07:36
  • In your xml there is no FloatingActionButton declared. And you are trying to use it in onCreate(). Either remove it from onCreate() method of your activity or declare it in xml layout. – Shweta Chauhan Jul 12 '17 at 08:05
  • yeah thank you so much but for fragment what i do because when i take it normal activity its work but when i use fragment its not work. (In normal activity -When i click on the login button its go next activity but when i select fragment its not go on next activity its showing the error) –  Jul 12 '17 at 08:10
  • what exactly you want to do? Don't understand. Want to go from fragment to activity? – Shweta Chauhan Jul 12 '17 at 08:24
  • Is it possible to go from one activity to another fragment? –  Jul 12 '17 at 08:27
  • Please tell me how it is? Give me a link or something which help me to correct the error and complete my project. Thank you! –  Jul 12 '17 at 08:35
  • https://stackoverflow.com/questions/35052383/android-support-v4-widget-drawerlayoutdrawerlistener-on-a-null-object-refere this same error occur in my project but don't understand how to resolve it. Please check it @Shweta –  Jul 12 '17 at 08:38
  • @KunalPitale I can help you but don't comment here because it's related another error – Shweta Chauhan Jul 12 '17 at 08:44
  • Okay madam @Shweta !! –  Jul 12 '17 at 08:53
  • Please tell me how to go from one activity to another fragment @Shweta –  Jul 12 '17 at 13:19
  • sorry but i am unable to see ur mail id . please provide me your mail id @shweta –  Jul 13 '17 at 06:22
0

Change your AppTheme like below

<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style>

Or add android:theme="@style/AppTheme.NoActionBar"> to your Activity

Ameer
  • 2,709
  • 1
  • 28
  • 44