可能导致错误的原因

时间:2019-11-20 10:10:51

标签: java android

在navigation.java中,错误:导航已在  编译单元,找不到符号方法  findNavController(Navigation ,int)

Navigation.java

package com.example.bcuilders;

import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

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

import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.Menu;

public class Navigation extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = findViewById(R.id.fab);
    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();
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.navigation, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}
}

并在mainActivity.java中出现错误:找不到符号类意图 mainActivity.java

 package com.example.bcuilders;

    import androidx.appcompat.app.AppCompatActivity;

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

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void buttonClick(View view)
    {

        EditText et1= (EditText) findViewById(R.id.MyEmail);
        EditText et2= (EditText) findViewById(R.id.MyPass);

        String email=et1.getText().toString();
        String pass=et2.getText().toString();

        String oemail="admin";
        String opass="admin";

        if (email.equals(oemail) && pass.equals(opass)){

            Intent intent = new intent(this, Navigation.class);
            startActivity(intent);

        }
        else{
            Toast toast = Toast.makeText(this, "Invalid Email/Pass",Toast.LENGTH_LONG);
            toast.show();
        }



    }
}

错误:导航已在编译单元中定义,并且无法在Navigation.java中找到符号方法findNavController(Navigation,int)

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

1)在mainActivity.java中出现错误:找不到符号类意图mainActivity.java

代替这个

Intent intent = new intent(this, Navigation.class);

使用此

Intent intent = new Intent(this, Navigation.class);

由于您使用的是小写意图,因此会引发该错误。

2)在navigation.java中,错误:导航已在编译单元中定义,并且找不到符号方法findNavController(Navigation,int)

要解决此错误,请将navigation.java重命名为导航以外的其他名称。对于您而言,它会尝试在您的findNavController(Navigation, int)类中找到navigation.java,而该类不存在。

希望它可以解决您的错误!