连接导航抽屉

时间:2017-07-12 07:15:14

标签: android xml android-layout android-actionbar

点击创建时出现此错误,然后转到注册

    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)

单击登录按钮时出错,但我不知道如何在android manifest.xml中使用HomeFragment.class(fragment)

    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

我在这个导航抽屉里拿着导航抽屉我有2个活动loginactivity和mainactivity。当我点击登录按钮然后它转到HomeFragement但它显示错误,当我点击创建一个然后它转到content_main(在这个content_main我有我的注册页面)。但这也显示了错误。

  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>

2 个答案:

答案 0 :(得分:0)

  

你用@ style / AppTheme.NoActionBar添加了主题,它改变了问题用@ 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>

答案 1 :(得分:0)

更改您的AppTheme,如下所示

<!-- 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>

或者将android:theme="@style/AppTheme.NoActionBar">添加到您的活动