Android Studio Clicklistner无法在抽屉式导航布局中工作

3pvhb19x  于 2022-12-30  发布在  Android
关注(0)|答案(1)|浏览(150)

当我点击主页或网站菜单时,没有任何事情发生或触发,即使我已经把吐司点击listner
这是我的代码请检查它帮助我.我已经上传了我的代码文件mainactivity.java,activity_main.xml和side_menu.xml检查代码,让我知道什么是错误,也解决了该错误.
第一个月

package com.example.collegeconnect;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

   DrawerLayout drawerLayout;
   NavigationView navigationView;
   Toolbar toolbar;
   TextView logintxt,drawerUsername,drawerEmail;

    private static String ipv4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ipv4 =getString(R.string.ipv4);

        toolbar = findViewById(R.id.Toolbar1);
        setSupportActionBar(toolbar);

        drawerLayout = findViewById(R.id.mainDrawer);
        navigationView = findViewById(R.id.navigationView);
        logintxt = findViewById(R.id.Logintxt);

        // defining headerview of drawer layout
        View headerView = navigationView.getHeaderView(0);
        drawerEmail  = headerView.findViewById(R.id.drawerEmail);
        drawerEmail.setText("19BEIT54013@vsitr.ac.in");
        drawerUsername = headerView.findViewById(R.id.drawerUsername);
        drawerUsername.setText("Jaivik22");

        navigationView.setNavigationItemSelectedListener(this);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);

        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

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

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        int id = item.getItemId();
        switch (id){
            case R.id.home22:
                Toast.makeText(getApplicationContext(),"Click on home",Toast.LENGTH_SHORT).show();
                break;
            case R.id.website:
                Toast.makeText(MainActivity.this,"Click on Website",Toast.LENGTH_SHORT).show();
                break;
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainDrawer"
    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"
    tools:context=".MainActivity"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    >
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:headerLayout="@layout/header"
        app:menu="@menu/sidemenu"
        android:layout_gravity="start"
        />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/Toolbar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/Logintxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Click to Login"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>



</androidx.drawerlayout.widget.DrawerLayout>

这是我的菜单项的XML文件。id和我在XML文件中声明的一样。
sidemenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <group android:checkableBehavior="single">

        <item
            android:id="@+id/home22"
            android:icon="@drawable/homeicon"
            android:title="Home" />

        <item
            android:id="@+id/website"
            android:icon="@drawable/internet"
            android:title="Website" />

    </group>

<!--    <item android:title="Course">-->
<!--        <menu>-->
<!--            <item-->
<!--                android:id="@+id/IT"-->
<!--                android:icon="@drawable/itbl"-->
<!--                android:title="Information Technology" />-->

<!--            <item-->
<!--                android:id="@+id/CSE"-->
<!--                android:icon="@drawable/csebl"-->
<!--                android:title="Computer Science Engineering"></item>-->

<!--            <item-->
<!--                android:id="@+id/CE"-->
<!--                android:icon="@drawable/ceblank"-->
<!--                android:title="Computer Engineering"></item>-->
<!--        </menu>-->
<!--    </item>-->

    <group android:checkableBehavior="single">
        <item android:title="Other">
            <menu>
                <item
                    android:id="@+id/rating"
                    android:icon="@drawable/rating"
                    android:title="rating" />

                <item
                    android:id="@+id/help"
                    android:icon="@drawable/help"
                    android:title="Help"></item>
            </menu>
        </item>
    </group>

    <item android:title="Settings"
        android:icon="@drawable/settings"
         android:id="@+id/settings"></item>

    <item android:title="Logout"
        android:icon="@drawable/logout"
        android:id="@+id/logout"
        >

    </item>
</menu>

我无法理解过去2天的问题,如果你们能帮忙,请告诉我

zbq4xfa0

zbq4xfa01#

将NavigationView放在DrawerLayout的末尾。
按以下顺序排列:

<DrawerLayout>
    <ConstraintLayout/>
    ...
    <NavigationView/>
</DrawerLayout>

相关问题