我有一个触摸监听器在我的 TextView 上。在触摸时,我登录Timber.i()
然后我打电话finish()
。如果在 finish() 之后,我再次启动我的应用程序,并再次单击 TextView,它将记录两次,然后 3 次,依此类推...
(如果我用普通的Log.i()替换Timber.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;
}
});
}
Layout :
<?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>
问题是你在活动中“种植”树木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"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)