2016-01-08 14 views
2

我使用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是firstActivity,在清单代码:

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

我在很多情况下测试,也许这引起我的手机,它没有在模拟器中泄漏,我会在其他一些Android手机中测试它。

+0

什么用'的getString(R.string.splash_time)'的时候发生的呢? –

+0

问题在这里'splashTime.setText(getString(R.string.splash_time,showTime));' –

+0

向我们展示您的代码在'getString(R.string.splash_time,showTime)' –

回答

0

这是魅族FlymeOS中的一个bug。只是排除它。详见我的PR

如果您不能等待合并这个PR,你可以做以下排除这种泄漏:

ExcludedRefs excludedRefs = AndroidExcludedRefs.createAppDefaults() 
      .staticField("android.app.Instrumentation", "mRecommendActivity") 
      .reason("Instrumentation would leak com.android.internal.app.RecommendActivity (in framework.jar) in Meizu FlymeOS") 
      .build(); 
LeakCanary.install(application, DisplayLeakService.class, excludedRefs);