2017-06-13 8 views
0

私はfirebase認証でログイン活動を実装する単純なアプリケーションを作っています。最初のiはアンドロイドスタジオで引き出しメニューを作り、それはまだ正常な、しかし、私は引き出しのメニューログイン名は次のようになったことをいくつかのクラスを追加したとき、私は私の引き出しを作る原因はこのようになってどこを知らない:アンドロイドスタジオでログインアクティビティを追加すると、ドロワーメニューがツールバーと別になります

enter image description here enter image description here

、これが私のMainActicityコードです:

super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    //setSupportActionBar(toolbar); 
    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.setDrawerListener(toggle); 
    toggle.syncState(); 

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

    displaySelectedScreen(R.id.nav_cari); 

    //get firebase auth instance 
    auth = FirebaseAuth.getInstance(); 

    //get current user 
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 

    authListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      FirebaseUser user = firebaseAuth.getCurrentUser(); 
      if (user == null) { 
       // user auth state is changed - user is null 
       // launch login activity 
       startActivity(new Intent(MainActivity.this, Login.class)); 
       finish(); 
      } 
     } 
    }; 

    progressBar = (ProgressBar) findViewById(R.id.progressBar); 

    if (progressBar != null) { 
     progressBar.setVisibility(View.GONE); 
    } 



} 

@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; 
} 

private void displaySelectedScreen (int itemId) { 
    Fragment fragment = null; // membuat objek dari kelas fragment 

    switch (itemId) { 
     case R.id.nav_cari: 
      fragment = new MenuPencarian(); 
      break; 
     case R.id.nav_penyewaanku: 
      fragment = new MenuPenyewaanku(); 
      break; 
     case R.id.nav_pemberitahuan: 
      fragment = new MenuPemberitahuan(); 
      break; 
     case R.id.nav_profilku: 
      fragment = new MenuProfil(); 
      break; 
     case R.id.nav_tentang: 
      fragment = new MenuTentangAplikasi(); 
      break; 
     case R.id.nav_bantuan: 
      fragment = new MenuBantuan(); 
      break; 
     case R.id.nav_keluar: 
      signOut(); 
      break; 
    } 
    //replacing the fragment 
    if (fragment != null) { 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     ft.replace(R.id.content_frame, fragment); 
     ft.commit(); 
    } 

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

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 

    displaySelectedScreen(item.getItemId()); 
    return true; 
} 

//sign out method 
public void signOut() { 
    auth.signOut(); 
} 


@Override 
public void onStart() { 
    super.onStart(); 
    auth.addAuthStateListener(authListener); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    if (authListener != null) { 
     auth.removeAuthStateListener(authListener); 
    } 

}` 

私login.java:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //Get Firebase auth instance 
    auth = FirebaseAuth.getInstance(); 

    if (auth.getCurrentUser() != null) { 
     startActivity(new Intent(Login.this, MainActivity.class)); 
     finish(); 
    } 
    setContentView(R.layout.activity_login); 
    input_email = (EditText)findViewById(R.id.email); 
    input_password = (EditText)findViewById(R.id.password); 
    buttonLogin = (Button)findViewById(R.id.btn_login); 
    buttonRegistrasi = (Button)findViewById(R.id.btn_registrasi); 
    buttonForgetPassword = (Button)findViewById(R.id.btn_reset_password); 
    progressBar1 = (ProgressBar)findViewById(R.id.progressBar); 

    //Get Firebase auth instance 
    auth = FirebaseAuth.getInstance(); 

    buttonRegistrasi.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(Login.this, Registrasi.class)); 
     } 
    }); 

    buttonForgetPassword.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //startActivity(new Intent(Login.this, ResetPasswordActivity.class)); 
     } 
    }); 

    buttonLogin.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String email = input_email.getText().toString(); 
      final String password = input_password.getText().toString(); 

      if (TextUtils.isEmpty(email)) { 
       Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show(); 
       return; 
      } 

      if (TextUtils.isEmpty(password)) { 
       Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show(); 
       return; 
      } 

      progressBar1.setVisibility(View.VISIBLE); 

      //authenticate user 
      auth.signInWithEmailAndPassword(email, password).addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() { 
         @Override 
         public void onComplete(@NonNull Task<AuthResult> task) { 
          // If sign in fails, display a message to the user. If sign in succeeds 
          // the auth state listener will be notified and logic to handle the 
          // signed in user can be handled in the listener. 
          progressBar1.setVisibility(View.GONE); 
          if (!task.isSuccessful()) { 
           // there was an error 
           if (password.length() < 6) { 
            input_password.setError(getString(R.string.minimum_password)); 
           } else { 
            Toast.makeText(Login.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show(); 
           } 
          } else { 
           Intent intent = new Intent(Login.this, MainActivity.class); 
           startActivity(intent); 
           finish(); 
          } 
         } 
        }); 
     } 
    }); 
} 

私login_layout.xml:

<LinearLayout 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:background="@drawable/mlg4" 
android:fitsSystemWindows="true" 
tools:context="com.example.meita.belajar_fragment2.Login"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="56dp" 
    android:paddingLeft="24dp" 
    android:paddingRight="24dp"> 

    <ImageView android:src="@drawable/orange" 
     android:layout_width="wrap_content" 
     android:layout_height="120dp" 
     android:layout_marginBottom="24dp" 
     android:layout_gravity="center_horizontal" /> 


     <EditText android:id="@+id/email" 
      android:layout_width="300dp" 
      android:layout_height="45dp" 
      android:layout_marginBottom="3dp" 
      android:layout_gravity="center" 
      android:maxLines="1" 
      android:ellipsize="end" 
      android:inputType="textEmailAddress" 
      android:background="@drawable/round_top_transparant" 
      android:hint="Email" 
      android:textColor="@color/putih" 
      android:textColorHint="@color/putih" 
      android:paddingLeft="15dp"/> 

     <EditText android:id="@+id/password" 
      android:layout_width="300dp" 
      android:layout_height="45dp" 
      android:layout_gravity="center" 
      android:maxLines="1" 
      android:ellipsize="end" 
      android:background="@drawable/round_bottom_transparant" 
      android:textColorHint="@color/putih" 
      android:textColor="@color/putih" 
      android:inputType="textPassword" 
      android:hint="Password" 
      android:paddingLeft="15dp"/> 

    <android.support.v7.widget.AppCompatButton 
     android:id="@+id/btn_login" 
     android:layout_width="300dp" 
     android:layout_height="45dp" 
     android:layout_marginTop="20dp" 
     android:layout_gravity="center" 
     android:background="@color/orange" 
     android:padding="12dp" 
     android:textColor="@color/hitam" 
     android:text="Login"/> 

    <Button android:id="@+id/btn_reset_password" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="30dp" 
     android:layout_marginBottom="2dp" 
     android:background="@null" 
     android:textColor="@color/putih" 
     android:text="Lupa password?" 
     android:layout_gravity="center" 
     android:textSize="16dip"/> 

    <Button android:id="@+id/btn_registrasi" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="2dp" 
     android:layout_marginBottom="24dp" 
     android:background="@null" 
     android:textColor="@color/putih" 
     android:text="Belum punya akun? Daftar Disini" 
     android:layout_gravity="center" 
     android:textSize="16dip"/> 

    <ProgressBar 
     android:id="@+id/progressBar" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_gravity="center|bottom" 
     android:layout_marginBottom="20dp" 
     android:visibility="gone" /> 

</LinearLayout> 

私activity_main.xml:

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:openDrawer="start"> 

<include 
    layout="@layout/app_bar_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
<android.support.design.widget.NavigationView 
    app:itemIconTint="@color/orange" 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_main" 
    app:menu="@menu/activity_main_drawer" /> 

私のstyles.xml:

<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> 
</style> 
<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> 
<item name="color">@color/orange</item> 

ありがとうございます。

+0

あなたのテーマshouldbe NoActionBarにこのテーマを追加します。マニフェストファイルに設定してください –

+0

あなたのAppThemeの親をLight.NoActionBarに置き換えてください – Bhavnik

+0

はい、その仕事は皆さんです – dazzle

答えて

0

すべてうまく見えます。代わりに、この

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

使用のTry->

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 

とマニフェストでは、あなたのMainActivity

<activity 
     android:name=".MainActivity" 
     ...... 
     android:theme="@style/AppTheme.NoActionBar"> 

</activity> 
関連する問題