有关Android导览(Android Navigation component)

Posted scruffybear

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有关Android导览(Android Navigation component)相关的知识,希望对你有一定的参考价值。

文章目录

小结

在使用android导览(Android Navigation component)碰到很多问题。解决了一些问题,但是“Skipped xxx frames! The application may be doing too much work on its main thread”这样的的问题一直没有解决。

有关Android导览(Android Navigation component)

参考Android Developer: Migrate to the Navigation component: Migrate to the Navigation component
Android Developer: Get started with the Navigation component,可以创建navigation graph方便地对Fragment进行管理。

碰到的问题

  • NavController cannot find an ID
    Caused by: java.lang.IllegalArgumentException: ID does not reference a View inside this Activity

  • java.lang.IllegalArgumentException: ID未引用此活动中的视图

  • xxx does not have a NavController set on xxx

对于碰到的以上的问题,都差不多,是获取Controller时的出现的问题,参考Android开发 报错: xxx does not have a NavController set on xxx,以下代码可以解决:

    @Override
    protected void onStart() 
        super.onStart();
        mController = Navigation.findNavController(MainActivity.this, R.id.nav_host_fragment);
    

或者:

    public void navigate(@IdRes int resId, @Nullable Bundle args) 
        try 
            navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
            mController = navHostFragment.getNavController();
            mController.navigate(resId, args);

         catch (IllegalStateException | IllegalArgumentException ex) 
            Log.e(TAG_NAV, String.format("Unable to navigate to destination. %s", ex.getMessage()));
        
    

对于以下碰到的问题始终没有解决:

  • Skipped xxx frames! The application may be doing too much work on its main thread

一种说法是创建多线程或者异步任务可以解决,但是试了一下好像不行。

另一种说法是Android应用运行时图片资源过大加载导致处理不过来。

  • mdpi = 1x
  • hdpi = 1.5x
  • xhdpi = 2x
  • xxhdpi = 3x
  • xxxhdpi = 4x

其中drawable-nodpi是不会进行缩放的,结果试了这种办法也是不行。
创建一个新的分辨率的资源如下:
yourprojectname->app->src->main->res-> New->Image Asset-> select your resolution image required

参考

Android Developer: Migrate to the Navigation component: Migrate to the Navigation component
Android Developer: Get started with the Navigation component
Stackoverflow: Why does my NavController cannot find an ID that I already have?
找不到.findNavController()的正确上下文
Android开发 报错: xxx does not have a NavController set on xxx
Stackoverflow: The application may be doing too much work on its main thread
13、主线程任务太多导致异常退出(The application may be doing too much work on its main thread)
Stackoverflow: Will a density qualified drawable folder or drawable-nodpi take precedence?

以上是关于有关Android导览(Android Navigation component)的主要内容,如果未能解决你的问题,请参考以下文章

phonegap android中navigator.geolocation.getCurrentLocation的奇怪行为

如何在 Android 10 中获取有关手机 IMEI 的信息?

Android Manifest 中有关 Action MAIN 和 Category Launcher 的信息

Android:如何将有关数字的数据从应用程序数据库提供到 android 手机应用程序 - 最近

Android,如何获取有关当前正在显示的活动的信息(在前台)?

Android-Android中service与application的生命周期有关系吗