小编Ral*_*ann的帖子

Dagger2组件具有多个依赖项

这是我目前拥有的,它的工作原理:

@FragmentScope
@Component(dependencies = {FacebookComponent.class}, 
           modules = {FragmentFacebookLoginModule.class})
public interface FragmentFacebookLoginComponent {

    void inject(FragmentFacebookLogin fragment);
}
Run Code Online (Sandbox Code Playgroud)

现在我想添加另一个依赖项.我改成了这个:

@Component(dependencies = {FacebookComponent.class, AnotherComponent.class}, 
           modules = {FragmentFacebookLoginModule.class})
Run Code Online (Sandbox Code Playgroud)

但现在我收到此错误消息:

FragmentFacebookLoginComponent依赖于多个作用域组件

我怎么解决这个问题?我怎样才能拥有多个依赖项?

如果我从一个组件中删除范围,我收到此错误消息:

AnotherComponent(unscoped)不能依赖于作用域组件

java android dagger-2

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

Dagger 2组件依赖项

是否可以将某些东西注入模块中?

我有2个基本模块/组件:

@Component(modules = {OkHttpModule.class})
public interface OkHttpComponent extends AppComponent {

    OkHttpClient provideOkHttpClient();
}

@Module
public class OkHttpModule {

    @Provides
    OkHttpClient provideOkHttpClient() {

        return mHttpClient;
    }
}
Run Code Online (Sandbox Code Playgroud)
@Component(modules = {GsonModule.class})
public interface GsonComponent extends AppComponent {

    Gson provideGson();
}

@Module
public class GsonModule {

    @Provides
    Gson provideGson() {

        return mGson;
    }
}
Run Code Online (Sandbox Code Playgroud)

有了这个模块/组件,我想创建第3个模块:

@Component(dependencies = {OkHttpComponent.class, GsonComponent.class}, 
           modules = {RetrofitModule.class})
public interface RetrofitComponent extends AppComponent {

    API provideAPI();
}

@Module
public class RetrofitModule {

    @Inject OkHttpClient mHttpClient;
    @Inject Gson         mGson; …
Run Code Online (Sandbox Code Playgroud)

java android dependency-injection dagger-2

11
推荐指数
1
解决办法
7601
查看次数

RxJava + Retrofit +民意调查

我有一个Retrofit电话,想要每30秒召回一次.为此,我使用了Observable.interval(0, 30, TimeUnit.SECONDS)

Observable
    .interval(0, 30, TimeUnit.SECONDS)
    .flatMap(x -> RestApi.instance().getUsers())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(list -> {
                    // ...
               },
               error -> Timber.e(error, "can't load users"));
Run Code Online (Sandbox Code Playgroud)

我的问题:如果api调用失败,onError则调用并且订阅取消订阅并且轮询不再有效:​​-(

为了捕获api错误,我添加了一个 retryWhen

Observable
    .interval(0, 30, TimeUnit.SECONDS)
    .flatMap(x -> RestApi.instance().getUsers()
                         .retryWhen(errors -> errors
                             .flatMap(error -> Observable.timer(15, TimeUnit.SECONDS))))
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(list -> {
                   // ...
               },
               error -> Timber.e(error, "can't load users"));
Run Code Online (Sandbox Code Playgroud)

这会捕获错误但我会在一段时间内收到多个api调用.每30秒我得到一个新的轮询信号,它以新的api请求结束.但是如果api请求失败,它会自行重试.所以我有一个新请求加上所有重试.

我的问题:如何在不取消订阅轮询信号的情况下处理api错误?

java android rx-java rx-android retrofit2

11
推荐指数
1
解决办法
4677
查看次数

DisplayLeakActivity不存在

有一段时间我有LeakCanary的问题(​​我认为自Android Studio 2.2以来,但不确定)

我添加了这个依赖项

 dependencies {
   debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
   releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
   testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
 }
Run Code Online (Sandbox Code Playgroud)

当我点击Android Studio中的播放按钮时,我收到以下错误消息:

$ adb shell am start -n "com.example.debug/com.squareup.leakcanary.internal.DisplayLeakActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Error while executing: am start -n "com.example.debug/com.squareup.leakcanary.internal.DisplayLeakActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.debug/com.squareup.leakcanary.internal.DisplayLeakActivity }
Error type 3
Error: Activity class {com.example.debug/com.squareup.leakcanary.internal.DisplayLeakActivity} does not exist.

Error while Launching activity
Run Code Online (Sandbox Code Playgroud)

但是当我用ClassyShark打开apk文件时,我可以看到活动就在那里.

在此输入图像描述

什么出错了?

android android-studio leakcanary android-studio-2.2

8
推荐指数
1
解决办法
1118
查看次数

IllegalArgumentException:不支持addAccount

我按照此说明添加了我的同步适配器.

但是有一个小问题:-(

当我打开设置 - >帐户 - >添加帐户并选择我的帐户时,我收到此错误消息

java.lang.IllegalArgumentException: addAccount not supported
    at android.accounts.AccountManager.convertErrorToException(AccountManager.java:2147)
    at android.accounts.AccountManager.-wrap0(AccountManager.java)
    at android.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:1993)
    at android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)
    at android.os.Binder.execTransact(Binder.java:453)
Run Code Online (Sandbox Code Playgroud)

看起来这次崩溃来自将Authenticator Java类转换为Kotlin类.

Java类就像这样锁定

public class Authenticator extends AbstractAccountAuthenticator {

    private final Context mContext;

    // Simple constructor
    public Authenticator(Context context) {

        super(context);
        mContext = context;
    }

    // Editing properties is not supported
    @Override
    public Bundle editProperties(AccountAuthenticatorResponse response,
                                 String accountType) {

        throw new UnsupportedOperationException();
    }

    @Override
    public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
                             String authTokenType, String[] requiredFeatures, Bundle options) …
Run Code Online (Sandbox Code Playgroud)

android android-syncadapter kotlin android-authenticator

7
推荐指数
1
解决办法
1233
查看次数

延迟 MockWebServer 响应

如何延迟 Square MockWebServer HTTP 响应?

有一种response.setBodyDelayTimeMs(...);方法,但这仅用于 SpdySocketHandler 而不是用于 HTTP 请求。

android square okhttp mockwebserver

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

使用Proguard时出现未知来源

如果我使用Proguard(minifyEnabled trueshrinkResources true)我的崩溃报告有点无用

这是Proguard的报告:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
    at xx.xxxx.xxx.xxxxx.xxxxxx.restoreViewAfterLoading(Unknown Source)
    at xx.xxxx.xxx.xxxxx.xxxxxx.newInstance(Unknown Source)
                                                     onCreateView
                                                     onViewCreated
                                                     access$000
    at xx.xxxx.xxx.xxxxx.xxxxxx$1.success(Unknown Source)
    at xx.xxxx.xxx.xxxxx.xxxxxx$1.success(Unknown Source)
    at retrofit.CallbackRunnable$1.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Run Code Online (Sandbox Code Playgroud)

这是没有Proguard的正常报告:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
    at xx.xxxx.xxx.xxxxx.xxxxxx.restoreViewAfterLoading(xxxxxx.java:123)
    at xx.xxxx.xxx.xxxxx.xxxxxx.access$000(xxxxxx.java:26)
    at xx.xxxx.xxx.xxxxx.xxxxxx$1.success(xxxxxx.java:96)
    at xx.xxxx.xxx.xxxxx.xxxxxx$1.success(xxxxxx.java:92)
    at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45) …
Run Code Online (Sandbox Code Playgroud)

android proguard minify shrinkresources

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

android:shape="line" 不可见

我有以下可绘制的形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="line" >

   <stroke
      android:width="1dp"
      android:color="#e0e0e0" />

</shape>
Run Code Online (Sandbox Code Playgroud)

并将其与此图像一起使用:

  <ImageView
     android:layout_width="match_parent"
     android:layout_height="1dp"
     android:layout_marginTop="8dp"
     android:src="@drawable/line" />
Run Code Online (Sandbox Code Playgroud)

当我设置android:layout_height为 1dp 时,形状不可见。如果android:layout_height设置为 2dp,则形状可见。

为什么我必须使用 2dp 的高度?

拉尔夫

android android-layout shapedrawable

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

PercentFrameLayout:您必须提供layout_width属性

我尝试使用PercentFrameLayout但我收到此错误消息:

Binary XML file line #: You must supply a layout_width attribute.
Run Code Online (Sandbox Code Playgroud)

这是我使用的xml:

<android.support.percent.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        app:layout_heightPercent="50%"
        app:layout_marginLeftPercent="25%"
        app:layout_marginTopPercent="25%"
        app:layout_widthPercent="50%"/>
</android.support.percent.PercentFrameLayout>
Run Code Online (Sandbox Code Playgroud)

这是一个错误还是我的错?

android android-layout android-percent-library android-percent-layout

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

如何更改camera2预览的纵横比?

我尝试更改Camera2预览的纵横比,但失败了 :-(

对于裁剪,我必须使用SCALER_CROP_REGION但我没有让它工作。

我使用Google的android-Camera2Video示例进行测试。

openCamera方法中,我添加了以下行:

mSensorSize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
Run Code Online (Sandbox Code Playgroud)

startPreview 中,我添加了以下内容:

final int centerX = mSensorSize.width() / 2;
final int centerY = mSensorSize.height() / 2;
final int cropSize = Math.min(mSensorSize.width(), mSensorSize.height());
final Rect crop = new Rect(centerY - cropSize / 2,
                           centerX - cropSize / 2,
                           cropSize,
                           cropSize);
mPreviewBuilder.set(CaptureRequest.SCALER_CROP_REGION, crop);
Run Code Online (Sandbox Code Playgroud)

我应该以 1:1 的比例预览,但它是 3:4 :-(

我做错了什么?

android android-camera2

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