相关疑难解决方法(0)

getApplication()与getApplicationContext()

我找不到令人满意的答案,所以我们在这里:有什么处理Activity/Service.getApplication()Context.getApplicationContext()

在我们的应用程序中,都返回相同的对象.ActivityTestCase然而,在一个getApplication()模拟应用程序将使用模拟getApplicationContext返回,但仍将返回一个不同的上下文实例(一个由Android注入).那是一个错误吗?这是故意的吗?

我甚至不了解首先的区别.测试套件外是否存在两个呼叫可能带有不同对象的情况?何时以及为何?此外,为什么getApplication定义上ActivityService,但不是Context?不应该随处可用的有效应用程序实例吗?

android android-service android-context android-activity

407
推荐指数
3
解决办法
30万
查看次数

在Android 7.0和Samsung设备上使用Dagger 2的RuntimeException

在我的Google Play控制台上,自从我开始使用Dagger 2以来,我看到了很多崩溃报告,但仅限于Android 7.0,主要是三星设备,一些Huawai和Motorola设备以及一些罕见的Xperia设备:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2984)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3045)
  at android.app.ActivityThread.-wrap14 (ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1642)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:154)
  at android.app.ActivityThread.main (ActivityThread.java:6776)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1518)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1408)
Caused by: java.lang.RuntimeException: 
  at dagger.android.AndroidInjection.inject (AndroidInjection.java:48)
  at dagger.android.support.DaggerAppCompatActivity.onCreate (DaggerAppCompatActivity.java:43)
  at com.package.MainActivity.onCreate (MainActivity.java:83)
  at android.app.Activity.performCreate (Activity.java:6956)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1126)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2927)
Run Code Online (Sandbox Code Playgroud)

我无法重现这个问题,因为我手边没有任何受影响的设备,似乎并非所有类型的设备都受到影响,更像是随机启动失败.

从我通过研究得到的结果是,在活动实际附加到应用程序之前,很可能会调用活动onCreate.但我无法证明这一说法......

我正在关注Google的MVP + Dagger架构蓝图.

我的应用类:

public class App extends DaggerApplication {

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected AndroidInjector<? extends DaggerApplication> …
Run Code Online (Sandbox Code Playgroud)

android samsung-mobile dagger-2 android-7.0-nougat

31
推荐指数
1
解决办法
3097
查看次数