1

我的应用程序开始handlerthread.start()在线崩溃。以下是完整的堆栈跟踪。

Caused by java.lang.OutOfMemoryError: Could not allocate JNI Env
       at java.lang.Thread.nativeCreate(Thread.java)
       at java.lang.Thread.start(Thread.java:1063)
       at br.com.gomus.androidapp.data.local.dao.artitist.ArtistDaoImpl.find(ArtistDaoImpl.java:96)
       at br.com.gomus.androidapp.data.local.repository.mediaplayer.music.MusicPlayerRepositoryImpl.getSongInfo(MusicPlayerRepositoryImpl.java:105)
       at br.com.gomus.androidapp.data.local.repository.mediaplayer.music.MusicPlayerRepositoryImpl.lambda$getMediaModel$4(MusicPlayerRepositoryImpl.java:68)
       at br.com.gomus.androidapp.data.local.repository.mediaplayer.music.-$$Lambda$MusicPlayerRepositoryImpl$fVfeeQmYGFkPz-ojRma96o5CY3o.call(lambda)
       at rx.Observable.unsafeSubscribe(Observable.java:10256)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:248)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:148)
       at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:77)
       at rx.internal.operators.OnSubscribeFilter$FilterSubscriber.onNext(OnSubscribeFilter.java:76)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.emitScalar(OperatorMerge.java:395)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.tryEmit(OperatorMerge.java:355)
       at rx.internal.operators.OperatorMerge$InnerSubscriber.onNext(OperatorMerge.java:846)
       at br.com.gomus.androidapp.data.local.repository.mediaplayer.music.MusicPlayerRepositoryImpl.lambda$null$6(MusicPlayerRepositoryImpl.java:90)
       at br.com.gomus.androidapp.data.local.repository.mediaplayer.music.-$$Lambda$MusicPlayerRepositoryImpl$hAG_T0swae9XDVx8RuKzvo4UVIs.call(lambda)
       at br.com.gomus.androidapp.data.local.dao.song.SongDaoImpl.lambda$findFirst$14(SongDaoImpl.java:207)
       at br.com.gomus.androidapp.data.local.dao.song.-$$Lambda$SongDaoImpl$g3Egrx7TYpSF8dU-ZLdsO2a2hF4.call(lambda)
       at br.com.gomus.androidapp.data.local.database.LocalDatabaseImpl.findFirst(LocalDatabaseImpl.java:420)
       at br.com.gomus.androidapp.data.local.dao.song.SongDaoImpl.findFirst(SongDaoImpl.java:205)
       at br.com.gomus.androidapp.data.local.repository.mediaplayer.music.MusicPlayerRepositoryImpl.lambda$onGetSong$7(MusicPlayerRepositoryImpl.java:84)
       at br.com.gomus.androidapp.data.local.repository.mediaplayer.music.-$$Lambda$MusicPlayerRepositoryImpl$eDMkgrqdvy5edHNcD0lzalXKrP0.call(lambda)
       at rx.Observable.unsafeSubscribe(Observable.java:10256)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:248)
       at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:148)
       at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:77)
       at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:224)
       at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230)
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
       at java.lang.Thread.run(Thread.java:818)

这是应用程序崩溃的查找方法。

handlerThread.start();线是崩溃点。也许我应该退出线程,但我不知道从哪里退出,因为这个类不是一个活动,所以它没有任何 onDestroy 或 onTerminate 方法。但我读到他们在运行方法结束时关闭了自己。

@Override
public void find(Func1<RealmQuery<Artist>, RealmQuery<Artist>> query,
                 Action1<Result<List<Artist>>> onResult) {
    if (query != null) {


        handlerThread = new HandlerThread("MyHandlerThreadArtist");
        handlerThread.start();
        looper = handlerThread.getLooper();

        if(looper == null)
            Looper.prepare();
        Handler handler = new Handler(looper);

        handler.post(new Runnable(){
            @Override
            public void run() {
                Timber.log(Log.DEBUG, "find Thread.currentThread().getName() " + Thread.currentThread().getName());

                mDatabase.findRealmObject(Artist.class, query,
                        result -> {
                            Timber.log(Log.DEBUG, "findRealmObject Thread.currentThread().getName() " + Thread.currentThread().getName());

                            onResult.call(Result.getData(Arrays.asList(result.data.toArray(new Artist[]{}))));

                        }, err -> onResult.call(Result.getError(err))
                );
            }
        });

    }
}

任何帮助,将不胜感激。提前致谢。

4

0 回答 0