InitLoader must be called on the main thread doInBackground

389 Views Asked by At

When i try to run function background Async Task i get following error saying below error.

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)

I call the background task 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"});
}

i have done little search and i don't know how i can solve this issue, and thanks in advance.

So how can i function in background Async task ?

How can I run loadVideos in background

0

There are 0 best solutions below