2017-08-09 6 views
1

私のTextViewにはonTouchListenerがあります。接触すると、私はTimber.i()でログし、finish()と呼んでいます。終了後()、私は再びアプリケーションを起動し、再度TextViewをクリックすると2回、次に3回などのログが表示されます。finish()を呼び出してアプリケーションを再起動した後のログの重複ログ

(Timber.i()を通常のログに置き換えた場合) I()、問題はありません)

// first time 
Clicked 

// second time 
Clicked 
Clicked 

// etc... 
Clicked 
Clicked 
Clicked 

木材バージョン:

compile 'com.jakewharton.timber:timber:4.5.1' 

の作業コード:

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Timber.plant(new Timber.DebugTree()); 

    TextView tv = (TextView) findViewById(R.id.mytextview); 
    tv.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      Timber.i("Clicked"); 
      finish(); 
      return false; 
     } 
    }); 
} 

レイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.caca.test.MainActivity"> 

    <TextView 
     android:id="@+id/mytextview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent"/> 

</android.support.constraint.ConstraintLayout> 
+0

はあなたsetDbugツリーなど –

答えて

2

問題は、アクティビティonCreateの方法でツリーを植えていることです。その代わりに、カスタムアプリケーションサブクラスを使用し、そこでツリーを植えます。


class MyApp : Application() { 

    override fun onCreate() { 
     super.onCreate() 

     if (BuildConfig.DEBUG) { 
      Timber.plant(DebugTree()) 
     } 
    } 
} 

し、それに応じてAndroidManifestを更新:

<application android:name="com.foo.MyApp" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"/>

+0

が、私はこの観察を追加したいアプリケーションクラスを投稿してください。 finish()を呼び出したにもかかわらず、アプリは実際に終了されず、Timberはまだ使用可能です。 @cwbbowronで述べたように、onCreateでTimber.plant()を呼び出すと別のロガーが追加されます。 Timber.treeCount()を使用して実行中のロガーの数を確認し、実行中のロガーの数を確認することもできます。これは静的メソッドであり、非常に便利だとわかりました。 – giulio

関連する問題