小编Gui*_*ira的帖子

Dagger2 + Kotlin:lateinit属性尚未初始化

我正在尝试将ViewModelFactory注入到我的Activity中,但它仍然抛出同样的错误:lateinit属性viewModelFactory尚未初始化.我找不到我可能做错的事.从我的课程中查看上面的代码

AppComponent.kt

@Component(modules = [(AppModule::class), (NetworkModule::class), (MainModule::class)])
interface AppComponent {

    fun inject(application: TweetSentimentsApplication)

    fun inject(mainActivity: MainActivity)

    fun context(): Context

    fun retrofit(): Retrofit
}
Run Code Online (Sandbox Code Playgroud)

MainModule.kt

@Module
class MainModule {

    @Provides
    fun mainViewModelFactorty(repository: TweetRepository): MainViewModelFactory = MainViewModelFactory(repository)

    @Provides
    fun local(database: AppDatabase): TweetLocal = TweetLocal(database)

    @Provides
    fun remote(tweetService: TweetService): TweetRemote = TweetRemote(tweetService)

    @Provides
    fun tweetService(retrofit: Retrofit): TweetService = retrofit.create(TweetService::class.java)

    @Provides
    fun repository(local: TweetLocal, remote: TweetRemote): TweetRepository = TweetRepository(local, remote)

}
Run Code Online (Sandbox Code Playgroud)

MainActivity.kt

class MainActivity : AppCompatActivity() {

    @Inject lateinit var viewModelFactory: MainViewModelFactory

    private val …
Run Code Online (Sandbox Code Playgroud)

android kotlin dagger-2

12
推荐指数
3
解决办法
1万
查看次数

Android + Firebase Auth + REST API:如何正确使用令牌?

我正在启动一个新项目,Firebase Auth 是身份验证的选择。这个想法是通过 Firebase 身份验证创建/登录用户,然后使用 Firebase ID 令牌在我的后端进行身份验证(通过身份验证标头)。

在 Google Samples 中,这是我应该获取令牌的方式:

FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
mUser.getIdToken(true)
    .addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
        public void onComplete(@NonNull Task<GetTokenResult> task) {
            if (task.isSuccessful()) {
                String idToken = task.getResult().getToken();
                // Send token to your backend via HTTPS
                // ...
            } else {
                // Handle error -> task.getException();
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

但是,如您所见,这是一个异步调用,因为它会转到 Firebase 服务器以获取令牌。所以,每次对后端的 REST API 调用,我都需要运行上面的代码,因为我不知道令牌何时过期。

有没有更好的方法来使用 Firebase Auth 安全调用我的后端 REST API?或者使用 Firebase ID 令牌是最好的一种?如果是这样,我应该如何为每个 REST API 调用包装这个 id 令牌获取?

如果您有更好的方法来验证用户稍后调用 rest apis,我会全力以赴。

api android restful-authentication firebase firebase-authentication

6
推荐指数
1
解决办法
1274
查看次数

OKHttp Authenticator 不适用于 Retrofit 暂停乐趣

我最近更新了 Retrofit to2.7.0和 OKHttp to3.14.4以利用 Retrofit 接口上的暂停乐趣。

除此之外,我还尝试为刷新令牌逻辑实现身份验证器。

这是改造界面

interface OfficeApi {
    @Authenticated
    @POST
    suspend fun getCharacter(): Response<CharacterResponse>
}
Run Code Online (Sandbox Code Playgroud)

这是我的身份验证器

class CharacterAuthenticator : Authenticator {

    override fun authenticate(
        route: Route?,
        response: Response
    ): Request? {
        if (responseCount(response) >= 2) return null

        return response.request()
                        .newBuilder()
                        .removeHeader("Authorization")
                        .addHeader("Authorization", "Bearer $newToken")
                        .build()

        return null
    }

    private fun responseCount(response: Response?): Int {
        var result = 1
        while (response?.priorResponse() != null) result++
        return result
    }

}
Run Code Online (Sandbox Code Playgroud)

这是改装有趣的电话

    override suspend fun getCharacter() …
Run Code Online (Sandbox Code Playgroud)

android authenticator okhttp retrofit2 kotlin-coroutines

6
推荐指数
1
解决办法
1352
查看次数

fitsSystemWindows没有使状态栏透明

我有一个ConstraintLayout,我想在其中包含折叠工具栏布局.所以,我遵循了这个教程:https://antonioleiva.com/collapsing-toolbar-layout/

但是,即使在我的XML中的几乎每个组件中包含fitsSystemWindows之后,我的状态栏也不透明.

这是我在活动中使用的风格

<style name="AppTheme2" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

XML布局太大了,所以我把它放在这个要点上:https://gist.github.com/guuilp/6e20ff9e00af8b85c858c5e832a17c34

此外,后退按钮也未显示.

android

5
推荐指数
1
解决办法
3678
查看次数

如何设置高度变化的动画

我的应用程序中有一张简单的卡片,当用户点击它时,我正在改变高度.这种高度的变化我想要动画,高度平滑变化.

@OnClick({R.id.emptyDescription})
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.emptyDescription:
            if(isCardCollapsed){

                emptyDescription.setMinimumHeight(400);

                cancelSaveLinear.setVisibility(View.VISIBLE);
                editDescription.setVisibility(View.GONE);

                isCardCollapsed = false;
            } else {
                emptyDescription.setMinimumHeight(100);

                cancelSaveLinear.setVisibility(View.GONE);
                editDescription.setVisibility(View.VISIBLE);

                isCardCollapsed = true;
            }

            break;
        case R.id.card_view:
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?

animation android

5
推荐指数
2
解决办法
5584
查看次数

如何在 Kotlin 中更改compareBy的顺序

我需要一个 Kotlin 中的比较器来对进入我的回收器视图中的每个对象进行排序(使用快速适配器)。

我需要按整数对对象进行排序,其中最大的排在前面。

上面的代码对进入列表的对象进行排序,但最大的对象位于列表的末尾。有办法颠倒顺序吗?

playersFastAdapter.itemAdapter.withComparator(compareBy({ it.player.goals }, {it.player.assists}), true)
Run Code Online (Sandbox Code Playgroud)

android comparable kotlin

5
推荐指数
1
解决办法
4057
查看次数

FirebaseMessagingService 11.6.0 HandleIntent

FirebaseMessagingService有方法onMessageReceived(),我们应该哪个override来处理notifications,但是这只能在应用程序中Foreground.

要处理notifications应用程序在后台时,我曾经覆盖handleIntent,只是调用onMessageReceived().

FirebaseMessagingService11.6.0中,该方法handleIntent成为最终的,据说,我无法像我一样覆盖它.

当我的应用程序在11.6.0中处于后台时,我该如何处理通知?

public class NotificationsListenerService extends FirebaseMessagingService {
    private static final String TAG = "NotificationsListenerService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) 

        String notifyData = remoteMessage.getData().get("notifData");

        if(notifyData.contains("|")){
            String[] itens = notifyData.split("\\|");
            notifyData = itens[0];
        }


        String notifyType = remoteMessage.getData().get("notifType");
        String title = remoteMessage.getData().get("title");
        String message = remoteMessage.getData().get("body");

        if(!isAppInForeground(App.getContext())){
            sendNotification(title, message, notifyData, notifyType);
        }
    }

    @Override
    public final void handleIntent(Intent intent) …
Run Code Online (Sandbox Code Playgroud)

android android-intent firebase firebase-cloud-messaging

5
推荐指数
1
解决办法
1万
查看次数

如何从OkHttp Response对象中提取原始JSON字符串?

private static final String URL = "http://www.livroandroid.com.br/livro/carros/carros_{tipo}.json";

public static List<Carro> getCarros(Context context, String tipo) throws IOException {
    String url = URL.replace("{tipo}", tipo);
    OkHttpClient okHttpClient = new OkHttpClient();
    Request request = new Request.Builder()
            .url(URL)
            .build();
    Response response = okHttpClient.newCall(request).execute();
    String json = response.body().toString();
    List<Carro> carros = parserJSON(context, json);
    return carros;
}
Run Code Online (Sandbox Code Playgroud)

如果我json在调用getCarros方法时打印出变量的值,我在logcat中看到以下消息:

com.squareup.okhttp.internal.http.RealResponseBody@1e11866

如何记录我收到的实际JSON字符串?

android json okhttp

3
推荐指数
1
解决办法
9205
查看次数