- Forum-Beiträge: 602
23.05.2021, 18:02:21 via Website
23.05.2021 18:02:21 via Website
Hallo zusammen
Ich versuche mich in Kotlin zu verbessern. Dazu arbeite ich mich durch ein Buch.
Das Beispiel, welches das Livecycle demonstriert, funktioniert bei mir nicht. Beim Compilieren entsteht folgender Fehler :
Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.0)
und
Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.0)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.0)
Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.0)
Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.0)
Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.0)
Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.0)
Duplicate class android.support.v4.os.ResultReceiver$1 found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.0)
Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.0)
Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-26.1.0-runtime (com.android.support:support-compat:26.1.0)
Go to the documentation to learn how to Fix dependency resolution errors.
Leider ist der Link nicht so aussage kräftig. Ich habe alle meine Codedateien zwei mal überprüft. Ich habe nichts anderes drin als das was im Buch steht. Und trotzdem funktioniert es nicht. Mein Problem ist, wo soll ich den Fehler suchen gehen. Am Code kann es nicht liegen, denn da wird nichts rot unterlegt.
Darum Meine Frage :
Wo soll ich nach einem möglichen Fehler suchen?
Gruss Renato
Vielleicht doch noch den Code. Könnte sein, dass ich doch was falsch gemacht habe.
MainActivity:
package ch.robbisoft.stopwatchdemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import ch.robbisoft.stopwatchdemo.databinding.ActivityMainBinding
import java.text.SimpleDateFormat
import java.util.*
class MainActivity : AppCompatActivity() {
private val dateFormat = SimpleDateFormat("HH:mm:ss:SSS", Locale.US)
init {
dateFormat.timeZone = TimeZone.getTimeZone("UCT")
}
private val model : StopWatchDemoViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val observer = StopWatchDemoLifecycleObserver(model)
model.running.observe(this, { running : Boolean? ->
running?.let {
binding.btnStart.setText(if (it) R.string.stop else R.string.start)
binding.btnStop.isEnabled = !it
}
})
model.diff.observe(this){ diff : Long? ->
diff?.let{
binding.txtMain.text = dateFormat.format(Date(it))
}
}
binding.btnStart.setOnClickListener {
model.running.value?.let { running ->
if(running){
observer.stop()
}else{
observer.scheduleAtFixedRate()
}
model.running.value = !running
}
}
binding.btnStop.setOnClickListener { model.diff.setValue(0L) }
lifecycle.addObserver(observer)
}
}
StopWatchDemoLifecycleObserver Klasse :
package ch.robbisoft.stopwatchdemo
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import java.util.*
class StopWatchDemoLifecycleObserver internal constructor(private val model : StopWatchDemoViewModel) : LifecycleObserver{
private lateinit var timer : Timer
private lateinit var timerTask : TimerTask
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun startTimer(){
timer = Timer()
val running = model.running.value ?: false
if (running){
scheduleAtFixedRate()
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun stopTimer() {
timer.cancel()
}
fun stop(){
timerTask.cancel()
}
fun scheduleAtFixedRate(){
val now = System.currentTimeMillis()
val diff = model.diff.value ?: now
model.started.value = now - diff
timerTask = object : TimerTask(){
override fun run() {
model.started.value?.let{
model.diff.postValue(System.currentTimeMillis() - it)
}
}
}
timer.scheduleAtFixedRate(timerTask, 0, 200)
}
}
StopWatchDemoViewModel Klasse :
package ch.robbisoft.stopwatchdemo
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class StopWatchDemoViewModel : ViewModel() {
val running = MutableLiveData(false)
val diff = MutableLiveData(0L)
val started = MutableLiveData(0L)
}
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.