1
我的應用程序出現內存泄漏問題,我閱讀了有關LeakCanary的內容,因此我試圖設置它。無法使用LeakCanary觀看片段(ClassCastException錯誤)
在我的應用程序中,我有一個活動,其中包含兩個片段,並且我試圖僅觀察第一個片段是否存在泄漏。這是我如何設置它:
我讓這個Java類包含LeakCanary邏輯。
public class ExampleApplication extends Application {
public static RefWatcher getRefWatcher(Context context) {
ExampleApplication application = (ExampleApplication) context.getApplicationContext();
return application.refWatcher;
}
private RefWatcher refWatcher;
@Override public void onCreate() {
super.onCreate();
refWatcher = LeakCanary.install(this);
}
}
我再將此添加到我的片段的的OnDestroy:
@Override public void onDestroy() {
super.onDestroy();
RefWatcher refWatcher = ExampleApplication.getRefWatcher(getActivity());
refWatcher.watch(MainFragment.this);
}
當我破壞了包含該片段的活動,我收到以下錯誤:
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to projectname.ExampleApplication
我已經廣泛搜索了Google和文檔,但找不到遇到的其他人這個錯誤。我不知道該怎麼做,但我真的很想使用這個工具。如果有人有任何建議,我會很感激。
非常weird..i具有完全相同的代碼,我不明白的是你的應用程序運行的Android版本的任何errors..in? – tsiro
minSdkVersion 19, targetSdkVersion 24,compileSdkVersion 24.這真的很奇怪,我會繼續討論它,看看我是否能夠實現它。 – user2805004
我剛更新泄漏金絲雀依賴測試我的應用程序在Android Marshmallow ... debugCompile'com.squareup.leakcanary:leakcanary-android:1.4' releaseCompile'com.squareup.leakcanary:leakcanary-android-no-op:1.4 ' testCompile'com.squareup.leakcanary:leakcanary-android-no-op:1.4' – tsiro