繁体   English   中英

我无法在我的导航抽屉上放置听众,您能告诉我我在哪里错吗?

[英]i can't make a listener on my navigation drawer, can you tell me where do i wrong?

我从空活动创建此drawer ,所以我不使用fragment

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

private FirebaseAuth.AuthStateListener authListener;
private FirebaseAuth auth;
private Button signOut;
private DrawerLayout nDrawer;
private ActionBarDrawerToggle nToggle;

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

当我放置此setNavigaionViewListener ,此活动将在构建时崩溃,我在某处出错吗?

    //setNavigationViewListner();

    //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, LoginActivity.class));
                finish();
            }
        }
    };

    signOut = (Button) findViewById(R.id.sign_out);
    nDrawer = (DrawerLayout) findViewById(R.id.drawer);
    nToggle = new ActionBarDrawerToggle(MainActivity.this,nDrawer,R.string.open,R.string.close);

    nDrawer.addDrawerListener(nToggle);
    nToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    signOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signOut();
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (nToggle.onOptionsItemSelected(item)){
        return true;
    }
    return super.onOptionsItemSelected(item);
}

//method to set Listener

private void setNavigationViewListner() {
    NavigationView navigationView = (NavigationView) findViewById(R.id.drawer);
    navigationView.setNavigationItemSelectedListener(this);

}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()){
        case R.id.account:{
            startActivity(new Intent(MainActivity.this, ProfileActivity.class));
            break;
        }

        case R.id.logout:{
            signOut();
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
            break;
        }
    }
    nDrawer.closeDrawer(GravityCompat.START);
    return true;
}

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

}

用这个

Toolbar toolbar = (Toolbar) findViewById(R.id.Yourtoolbar);   
nToggle = new ActionBarDrawerToggle(
       MainActivity.this, nDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

代替这个

nToggle = new ActionBarDrawerToggle(MainActivity.this,nDrawer,R.string.open,R.string.close);

并同时更改此删除DrawerLayout findViewById(R.id.drawer)并将您的NavigationView ID

例如findViewById(R.id.YournavigationView);

NavigationView navigationView = (NavigationView) findViewById(R.id.YournavigationView);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM