Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试使用改型发出get请求时,无法为类java.lang.Object创建调用适配器_Android_Retrofit_Retrofit2_Dagger Hilt_Android Paging 3 - Fatal编程技术网

Android 尝试使用改型发出get请求时,无法为类java.lang.Object创建调用适配器

Android 尝试使用改型发出get请求时,无法为类java.lang.Object创建调用适配器,android,retrofit,retrofit2,dagger-hilt,android-paging-3,Android,Retrofit,Retrofit2,Dagger Hilt,Android Paging 3,我在尝试向NewsApi.org api发出get请求时遇到以下错误。根据他们的文档,一切似乎都是正确的,我的设置与我在github上找到的许多教程和一些示例基本相同 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.newsfeed, PID: 12360 java.lang.IllegalArgumentException: Unable to create call adapter for class

我在尝试向NewsApi.org api发出get请求时遇到以下错误。根据他们的文档,一切似乎都是正确的,我的设置与我在github上找到的许多教程和一些示例基本相同

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.newsfeed, PID: 12360
    java.lang.IllegalArgumentException: Unable to create call adapter for class java.lang.Object
        for method NewsApi.getBreakingNews
        at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
        at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:234)
        at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:160)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
        at java.lang.reflect.Proxy.invoke(Proxy.java:393)
        at $Proxy1.getBreakingNews(Unknown Source)
        at com.example.newsfeed.data.API.NewsApi$DefaultImpls.getBreakingNews$default(NewsApi.kt:11)
        at com.example.newsfeed.util.NewsPagingSource.load(NewsPagingSource.kt:25)
        at androidx.paging.PageFetcherSnapshot.doInitialLoad(PageFetcherSnapshot.kt:275)
        at androidx.paging.PageFetcherSnapshot$pageEventFlow$1.invokeSuspend(PageFetcherSnapshot.kt:160)
        at androidx.paging.PageFetcherSnapshot$pageEventFlow$1.invoke(PageFetcherSnapshot.kt)
        at androidx.paging.CancelableChannelFlowKt$cancelableChannelFlow$1.invokeSuspend(CancelableChannelFlow.kt:30)
        at androidx.paging.CancelableChannelFlowKt$cancelableChannelFlow$1.invoke(CancelableChannelFlow.kt)
        at androidx.paging.SimpleChannelFlowKt$simpleChannelFlow$1$1$producer$1$1.invokeSuspend(SimpleChannelFlow.kt:57)
        at androidx.paging.SimpleChannelFlowKt$simpleChannelFlow$1$1$producer$1$1.invoke(SimpleChannelFlow.kt)
        at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:91)
        at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:194)
        at androidx.paging.SimpleChannelFlowKt$simpleChannelFlow$1$1$producer$1.invokeSuspend(SimpleChannelFlow.kt:52)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.EventLoop.processUnconfinedEvent(EventLoop.common.kt:69)
        at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:357)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:27)
        at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:110)
        at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:158)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
        at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:49)
        at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
        at androidx.lifecycle.BlockRunner.maybeRun(CoroutineLiveData.kt:174)
        at androidx.lifecycle.CoroutineLiveData.onActive(CoroutineLiveData.kt:240)
        at androidx.lifecycle.LiveData.changeActiveCounter(LiveData.java:390)
        at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:466)
        at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:425)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
        at androidx.fragment.app.FragmentViewLifecycleOwner.handleLifecycleEvent(FragmentViewLifecycleOwner.java:88)
        at androidx.fragment.app.Fragment.performStart(Fragment.java:3028)
        at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:589)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:300)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
        at androidx.fragment.app.FragmentManager.moveToS

问题似乎出在我的api接口上,但如果有用的话,我将分享更多的代码。目前我的api接口如下:

package com.example.newsfeed.data.API

import com.example.newsfeed.data.API.ApiKey.Companion.API_KEY
import com.example.newsfeed.data.models.NewsApiResponse
import retrofit2.http.GET
import retrofit2.http.Query

interface NewsApi {

    @GET("/v2/top-headlines")
    suspend fun getBreakingNews(
        @Query("country")
        country: String = "us",
        @Query("page")
        pageNum: Int = 1,
        @Query("apiKey")
        apiKey: String = API_KEY,
        @Query("pageSize")
        pageSize: Int
    ): NewsApiResponse

}
刀柄注射用网络模块:

package com.example.newsfeed.di

import com.example.newsfeed.data.API.NewsApi
import com.example.newsfeed.util.Constants.Companion.BASE_URL
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {

    @Singleton
    @Provides
    fun provideRetrofit(): Retrofit =
        Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()


    @Provides
    @Singleton
    fun provideNewsApi(retrofit: Retrofit): NewsApi =
        retrofit.create(NewsApi::class.java)
}

我的分页源设置:

package com.example.newsfeed.util

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.bumptech.glide.load.HttpException
import com.example.newsfeed.data.API.NewsApi
import com.example.newsfeed.data.models.Article
import com.example.newsfeed.util.Constants.Companion.NEWSAPI_STARTING_PAGE_INDEX
import java.io.IOException


class NewsPagingSource(
    private val newsapi: NewsApi
): PagingSource<Int, Article>() {

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Article> {
        val position = params.key ?: NEWSAPI_STARTING_PAGE_INDEX

        return try {

            val response = newsapi.getBreakingNews(pageNum = position, pageSize = params.loadSize)
            val articles = response.articles

            LoadResult.Page(
                data = articles,
                prevKey = if (position == NEWSAPI_STARTING_PAGE_INDEX) null else position - 1,
                nextKey = if (articles.isEmpty()) null else position + 1
            )
        } catch (exception: IOException) {
            LoadResult.Error(exception)
        } catch (exception: HttpException) {
            LoadResult.Error(exception)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, Article>): Int? {
        return state.anchorPosition?.let { anchorPosition ->
            state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
                ?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
        }
    }

}

视图模型:

package com.example.newsfeed.viewmodels


import androidx.lifecycle.ViewModel
import com.example.newsfeed.repository.NewsRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import javax.inject.Inject

@HiltViewModel
class NewsScreenViewModel @Inject constructor(
    val newsRepository: NewsRepository,
    private val ApplicationScope: CoroutineScope
): ViewModel() {

    val articles = newsRepository.getBreakingNews()

}
片段:

package com.example.newsfeed.ui

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.example.newsfeed.R
import com.example.newsfeed.adapters.NewsArticlesAdapter
import com.example.newsfeed.databinding.FragmentNewsScreenBinding
import com.example.newsfeed.viewmodels.NewsScreenViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class NewsScreenFragment : Fragment(R.layout.fragment_news_screen) {

    private val viewModel: NewsScreenViewModel by viewModels()

    private var _binding: FragmentNewsScreenBinding? = null
    private val binding get() = _binding!!

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        _binding = FragmentNewsScreenBinding.bind(view)

        val adapter = NewsArticlesAdapter()

        binding.apply {
            newsRecyclerView.setHasFixedSize(true)
            newsRecyclerView.adapter = adapter
        }

        viewModel.articles.observe(viewLifecycleOwner) {
            adapter.submitData(viewLifecycleOwner.lifecycle, it)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}


您正在运行一个旧版本的改型,而不支持协同程序


要启用对
suspend
功能的支持,请将改型升级到
2.6.0
或更高版本。

我之所以能够确定这一点,是因为
ServiceMethod.java
中的大部分代码在
2.5.0
版本中被重构为其他类,而该文件现在只有43行。是的,就是这样!显然,在导入我的依赖项时,我从他们的github复制了稳定版本的改型,而不是最新版本……如果不是你的话,我可能要花很多时间才能弄明白这一点。谢谢
package com.example.newsfeed.ui

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.example.newsfeed.R
import com.example.newsfeed.adapters.NewsArticlesAdapter
import com.example.newsfeed.databinding.FragmentNewsScreenBinding
import com.example.newsfeed.viewmodels.NewsScreenViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class NewsScreenFragment : Fragment(R.layout.fragment_news_screen) {

    private val viewModel: NewsScreenViewModel by viewModels()

    private var _binding: FragmentNewsScreenBinding? = null
    private val binding get() = _binding!!

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        _binding = FragmentNewsScreenBinding.bind(view)

        val adapter = NewsArticlesAdapter()

        binding.apply {
            newsRecyclerView.setHasFixedSize(true)
            newsRecyclerView.adapter = adapter
        }

        viewModel.articles.observe(viewLifecycleOwner) {
            adapter.submitData(viewLifecycleOwner.lifecycle, it)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}