如何解决Android MultiDex Apllication Instance Null Pointer Exception?

How to solve Android MultiDex Apllication Instance Null Pointer Exception?

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

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;
    }
}

然后我从这个核心创建一个实例class。但是这里我的常量 getIntance 值 returns null.

TestApplication.getInstance().setupRetrofit();

我不明白。 GetInstance 方法总是 return null;

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

如何解决这种情况?

更改 getInstance()

由此

public static TestApplication getInstance() {
return instance;
}

至此

public static TestApplication getInstance() {
instance=new TestApplication();
return instance;
}

并从 oncreate

中删除这一行
instance = this;

永远不要像这样使用应用程序实例。要获取应用程序实例,您需要 activity,在您的 activity 中,您可以使用

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

这将解决您的问题,但您实施改造和使用它的方式并不是最佳做法。您可以找到不需要 activity.

的更好方法

启用 Multidex 后

替换为:-

public class 应用程序扩展了 MultiDexApplication {

public static final String TAG = Application.class.getSimpleName();

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

    ApplicationHelper.initDatabaseHelper(this);
    PostInteractor.getInstance(this).subscribeToNewPosts();
}

}