java - 如何解决Android MultiDex应用实例空指针异常?

标签 java android android-multidex

我想在我的 Android 项目中进行核心 API 服务集成。 我的核心服务类如下:

public class TestApplication extends MultiDexApplication {
    private static final String LOG_TAG_NETWORK = "GNetwork";
    public static JacksonConverterFactory factory;
    private static TestApplication instance;
    private OkHttpClient.Builder okHttpBuilder;
    private TestService testtService;
    private Location lastKnownLocation;

public static TestApplication getInstance() {
    return instance;
}

@Override
public void onCreate() {
    super.onCreate();
    instance = this;

    Pref.init(this);

    okHttpBuilder = new OkHttpClient.Builder();
    okHttpBuilder.connectTimeout(UrlConstants.CONNECT_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)
            .readTimeout(UrlConstants.READ_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)
            .writeTimeout(UrlConstants.WRITE_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
    if (BuildConfig.LOG_ENABLED) {
        HttpLoggingInterceptor httpLoggingInterceptor =
                new HttpLoggingInterceptor(message -> Log.d(LOG_TAG_NETWORK, message));
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        okHttpBuilder.addInterceptor(httpLoggingInterceptor);
    }
    factory = getJacksonConverterFactory();
}

@NonNull
private JacksonConverterFactory getJacksonConverterFactory() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return JacksonConverterFactory.create(objectMapper);
}

public void setupRetrofit() {

    String BASE_URL = "http://xxxxx";

    Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).client(okHttpBuilder.build())
            .addConverterFactory(factory).build();

    testService = retrofit.create(TestService.class);
}

public TestService getTestService() {
    return testService;
    }
}

然后我从这个核心类创建一个实例。但这里我的常量 getIntance 值返回 null。

TestApplication.getInstance().setupRetrofit();

我无法理解这一点。 GetInstance 方法始终返回 null

在我的应用中 build.gradle 文件是 multiDexEnabled true

这种情况如何解决?

最佳答案

永远不要使用这样的应用程序实例。为了获取应用程序实例,您需要 Activity ,并且在您的 Activity 中您可以使用

    static TestApplication getInstance(Activity activity) {
        if (instance == null)
            instance = ((TestApplication) activity.getApplication());
        return instance;
    }

这将解决您的问题,但您实现改造和使用它的方式不是最佳实践。您可以找到不需要 Activity 的更好方法。

关于java - 如何解决Android MultiDex应用实例空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60238021/

相关文章:

android - 错误 ':app:packageAllDebugClassesForMultiDex'。 > java.util.zip.ZipException : duplicate entry:

java - Jpa 存储库查询 - java.lang.Object;不能转换到模型

java - Junit 测试装置 - 初始化变量

android - Android 中的 cURL SSL 通信等效项

android - 错误:com. android.dex.DexException: Multiple dex files define Landroid/support/design/widget/CoordinatorLayout$HierarchyChangeListener

android - 在路径 : DexPathList 上找不到类 "android.support.multidex.MultiDexApplication"

java - 将 XHTML 文件中的 PDF 文件附加到电子邮件

java - spring amqp注解驱动一个队列两个监听器区分路由key

android - SecurityException 阻止我通过将图像的 URI 存储在 SQLIte 数据库中来显示图像

android - 将 Holo 主题与 AppCompatActivity 结合使用