in android, Java

有关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?

Write a Comment

Comment