首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >找不到.findNavController()的正确上下文

找不到.findNavController()的正确上下文
EN

Stack Overflow用户
提问于 2020-04-10 05:29:39
回答 1查看 85关注 0票数 0

所以,我正在开发一个安卓应用程序等。我有一个带bottomNavView的Main2Activity,它可以帮助我在ProfileFragment和HomeFragment之间切换。问题是我想在我的ProfileFragment中使用另一个navView,我模拟了某种TabView,这样我就可以在我的ProfileFragment中看到两个不同的片段。

这就是我的Main2Activity

代码语言:javascript
复制
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()中使用的上下文:

代码语言:javascript
复制
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未引用此活动中的视图

有没有人?

更新:这是堆栈跟踪

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2020-07-28 15:42:25

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61130527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档