0

所以我试图在 AIDE 中制作抽屉导航,但在开始编写按钮功能或片段后,我不断收到这些错误。

出于某种原因,我找不到并实现 onNavigationItemSelectedListener 并导致这两个错误

MainActivity.java
Unknown type 'onNavigationItemSelectedListener' of 'android.support.design.widget.
NavigationView'

Method 'android.support.design.widget.NavigationView.setNavigationItemSelected Lis
tener(android.support.design.widget.Navigat
ionView.OnNavigationItemSelectedListener)'
in 'android.support.design.widget. NavigationView' can not be applied to
'(com.apple.leapp.MainActivity)'

MainActivity.java

package com.apple.leapp;

import android.app.*;
import android.os.*;
import android.support.v7.app.*;
import android.widget.*;
import android.content.*;
import android.support.v7.widget.*;
import android.view.View;
import android.support.v4.widget.*;
import android.support.v4.view.*;
import android.support.design.widget.NavigationView;
import android.support.annotation.*;
import android.view.*;

public class MainActivity extends AppCompatActivity implements NavigationView.onNavigationItemSelectedListener
{
    private DrawerLayout drawer;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTitle("Main Page");
        
        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbr);
        setSupportActionBar(toolbar);
        
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.bringToFront();
        navigationView.setNavigationItemSelectedListener(this);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
        R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        
        if(savedInstanceState == null){
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment01,
             new IdiotFragment()).commit();
        navigationView.setCheckedItem(R.id.nav_rouge);}
    }
    @Nullable
    @Override
    public boolean onNavigationItemSelectedListener(@NonNull MenuItem item){
        switch (item.getItemId()){
            case R.id.nav_rouge:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment01,
               new IdiotFragment()).commit();
            break;
            case R.id.nav_quoge:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment01,
               new TicFragment()).commit();
            break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    
    @Override
    public void onBackPressed()
    {
        if (drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        } else {
        super.onBackPressed();
        }
    }
    
    
    
}

这是我的毕业典礼

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion "27.+"

    defaultConfig {
        applicationId "com.apple.leapp"
        minSdkVersion 14
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:27'
    compile 'com.android.support:design:+'
    compile 'com.android.support:appcompat-v7:27'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    
}

我试着四处寻找解决方案,但我找不到,所以我试着在这里问。

4

1 回答 1

0

尝试替换NavigationView.onNavigationItemSelectedListenerNavigationView.OnNavigationItemSelectedListener. 请注意 中的大写字母OnNavigationItemSelectedListenerO。

于 2021-04-21T19:42:15.907 回答