我使用 LeakCanary 来测试我的应用程序。它显示泄漏,但我无法修复它。
public class SplashActivity extends Activity {
private TextView splashTime;
private Long showTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
init();
}
public void init() {
showTime = 1L;
splashTime = (TextView) findViewById(R.id.splash_time);
splashTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enterApp();
}
});
splashTime.setText(getString(R.string.splash_time, showTime));
Log.d("splash", "init");
}
public void enterApp() {
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
finish();
}}
金丝雀日志在这里:
D/LeakCanary: In com.less.haku.hcomic:1.0:1.
* com.less.haku.hcomic.SplashActivity has leaked:
* GC ROOT static android.app.Instrumentation.mRecommendActivity
* references android.app.Instrumentation$RecommendActivity.mTarget
* leaks com.less.haku.hcomic.SplashActivity instance
* Reference Key: f4ea7ad3-a212-439f-ba67-3557823b07e9
* Device: Meizu Meizu m2 note m2 note
* Android Version: 5.1 API: 22
* Durations: watch=5014ms, gc=176ms, heap dump=2598ms, analysis=23065ms
我不知道它是怎么发生的,活动非常简单,没有处理程序,没有内部类。而且canaryLog看不懂,怎么追查问题?感谢帮助
我使用 string.xml 连接字符串。所有的 string.xml 代码都在这里:
<resources>
<string name="app_name">HComic</string>
<string name="action_settings">Settings</string>
<string name="splash_time">less %1$ds</string>
</resources>
SplashActivity 是第一个活动,在清单代码中:
<activity
android:screenOrientation="portrait"
android:name=".SplashActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我在很多情况下都测试过,可能是我的手机造成的,它没有在模拟器中泄漏,我会在其他一些安卓手机上测试它。