代码之家  ›  专栏  ›  技术社区  ›  sanoj lawrence

initLoader必须在主线程上调用

  •  0
  • sanoj lawrence  · 技术社区  · 4 年前

    当我尝试运行函数后台时 Async Task 我得到下面的错误说下面的错误。

    2021-10-07 11:50:10.045 21569-21636/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.example.jlplayerfilepicker, PID: 21569
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:365)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:784)
     Caused by: java.lang.IllegalStateException: initLoader must be called on the main thread
        at androidx.loader.app.LoaderManagerImpl.initLoader(LoaderManagerImpl.java:412)
        at filepicker.filter.FileFilter.getFiles(FileFilter.java:19)
        at Activity.SplashScreen.loadVideos(SplashScreen.java:142)
        at Activity.SplashScreen.access$200(SplashScreen.java:39)
        at Activity.SplashScreen$LoadVideos.doInBackground(SplashScreen.java:136)
        at Activity.SplashScreen$LoadVideos.doInBackground(SplashScreen.java:121)
        at android.os.AsyncTask$2.call(AsyncTask.java:345)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
        at java.lang.Thread.run(Thread.java:784)
    

    我称之为后台任务 new LoadVideos().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    @SuppressLint("StaticFieldLeak")
    private class LoadVideos extends AsyncTask<Void, Integer, Void> {
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            startActivity(new Intent(SplashScreen.this, MainActivity.class));
        }
    
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }
    
        @Override
        protected Void doInBackground(Void... voids) {
            loadVideos();
            return null;
        }
    }
    
    private void loadVideos() {
        FileFilter.getFiles(this, directories -> {
            for (Directory<NormalFile> directory : directories) {
                //list.addAll(directory.getFiles());
                for (int i = 0; i < directory.getFiles().size(); i++) {
                    File file = new File(directory.getFiles().get(i).getPath());
                    Log.d(TAG, "onResult: " + file.getPath());
                    FileStore fs = db.getFileByCustomPath(file.getAbsolutePath());
                    AllVideoModel avm = new AllVideoModel();
                    avm.setFileName(file.getName());
                    avm.setVideoPath(file.getAbsolutePath());
                    avm.setFolderName(Objects.requireNonNull(file.getParentFile()).getName());
                    avm.setVideoSize(file.length());
                    avm.setParentPath(file.getParent());
                    StaticConstant.allMediaListWithModel.add(avm);
                    StaticConstant.FABbeforeSort.add(avm);
                    if (fs == null) {
                        //Add to DataBase
                        FileStore fsNew = new FileStore();
                        fsNew.setName(file.getName());
                        fsNew.setFilePath(file.getAbsolutePath());
                        fsNew.setParent(Objects.requireNonNull(file.getParentFile()).getName());
                        fsNew.setUpdatedAt(new Date());
                        fsNew.setParentPath(file.getParent());
                        db.insertOrUpdateFile(fsNew);
                    }
                }
            }
        }, new String[]{".mp4",".ts",".mkv",".mov",
                ".3gp",".mv2",".m4v",".webm",".mpeg1",".mpeg2",".mts",".ogm",
                ".bup", ".dv",".flv",".m1v",".m2ts",".mpeg4",".vlc",".3g2",
                ".avi",".mpeg",".mpg",".wmv",".asf"});
    }
    

    我做了很少的搜索,我不知道如何解决这个问题,提前感谢。

    那么我如何在后台运行 Async task ?

    我该怎么跑 loadVideos 在后台

    0 回复  |  直到 4 年前
    推荐文章