无法找到 .findNavController() 的适当上下文。

huangapple 未分类评论44阅读模式
英文:

Can't find proper context for .findNavController()

问题

这是我的Main2Activity:

public class Main2Activity extends AppCompatActivity{
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        BottomNavigationView navView = findViewById(R.id.nav_view);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_settings, R.id.navigation_new_post,
                R.id.navigation_notifications, R.id.navigation_profile)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

    public BottomNavigationView getBottomNavigationView(){
        return findViewById(R.id.nav_view);
    }

    public Activity getActivity(){
        return this;
    }
}

这是我的ProfileFragment,问题出在这里。我找不到在findNavController()和setupWithNavController()中使用的上下文:

public class ProfileFragment extends Fragment {

    private ProfileViewModel notificationsViewModel;
    private boolean isLoading = false;

    public ProfileFragment() {
    }

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel = ViewModelProviders.of(this).get(ProfileViewModel.class);
        View root = inflater.inflate(R.layout.fragment_profile, container, false);

        BottomNavigationView navView = root.findViewById(R.id.profile_nav_view);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_my_posts, R.id.nav_applied_to)
                .build();
        NavController navController = Navigation.findNavController(getActivity(), R.id.profile_nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(new Main2Activity(), navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);

        return root;
    }
}

我一直收到的错误是:java.lang.IllegalArgumentException: ID does not reference a View inside this Activity

有人可以帮忙吗?

更新:这是堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.heirup, PID: 17523
    java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
        at android.app.Activity.requireViewById(Activity.java:2772)
        at androidx.core.app.ActivityCompat.requireViewById(ActivityCompat.java:363)
        at androidx.navigation.Navigation.findNavController(Navigation.java:58)
        at com.example.heirup.ui.profile.ProfileFragment.onCreateView(ProfileFragment.java:38)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7081)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
I/Process: Sending signal. PID: 17523 SIG: 9
英文:

So, I'm working on an Android app etc. I have this Main2Activity with a bottomNavView that helps me switch between ProfileFragment and HomeFragment. The thing is I want to use another navView inside my ProfileFragment, I simulated some kind of TabView so I can see two different fragments inside my ProfileFragment.

So, this is my Main2Activity

public class Main2Activity extends AppCompatActivity{
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        BottomNavigationView navView = findViewById(R.id.nav_view);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_settings, R.id.navigation_new_post,
                R.id.navigation_notifications, R.id.navigation_profile)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

    public BottomNavigationView getBottomNavigationView(){
        return findViewById(R.id.nav_view);
    }

    public Activity getActivity(){
        return this;
    }
}

This is my ProfileFragment, and here's where the problem is. I can't find the context to use in findNavController() and setupWithNavController():

public class ProfileFragment extends Fragment {

    private ProfileViewModel notificationsViewModel;
    private boolean isLoading = false;

    public ProfileFragment() {
    }

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel = ViewModelProviders.of(this).get(ProfileViewModel.class);
        View root = inflater.inflate(R.layout.fragment_profile, container, false);



        BottomNavigationView navView = root.findViewById(R.id.profile_nav_view);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_my_posts, R.id.nav_applied_to)
                .build();
        NavController navController = Navigation.findNavController(getActivity(), R.id.profile_nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(new Main2Activity(), navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);

        return root;
    }

}

The error I keep getting: java.lang.IllegalArgumentException: ID does not reference a View inside this Activity

Anyone?

UPDATE: This is the stacktrace

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.heirup, PID: 17523
    java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
        at android.app.Activity.requireViewById(Activity.java:2772)
        at androidx.core.app.ActivityCompat.requireViewById(ActivityCompat.java:363)
        at androidx.navigation.Navigation.findNavController(Navigation.java:58)
        at com.example.heirup.ui.profile.ProfileFragment.onCreateView(ProfileFragment.java:38)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7081)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
I/Process: Sending signal. PID: 17523 SIG: 9


答案1

得分: 0

使用带有参数 root.findViewById(#你的nav_host id)findNavController(View v)

英文:

Use findNavController(View v) with parameter root.findViewById(#your nav_host id)

huangapple
  • 本文由 发表于 2020年4月10日 05:29:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/61130527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定