每当我尝试运行我的程序时,它都会在subscription = CompositeDisposable()
上崩溃。我不知道问题是什么,但当我运行直接从GitHub导入的同一个项目时,它工作正常,当我在我自己的项目中实现它时,就像onClickListener调用的一个活动,它&#39 ; s崩溃在subscription = CompositeDisposable()。
我附加了我的Kotlin课程,Gradle(app)和Gradle(项目)。我也把Kotlin与Java混合在一起,但是除了这个Kotlin类之外,一切都工作得非常好,我从我的Java类中调用了这个Kotlin类。
这是我导入的库的链接,当单独运行样本时,它运行良好https://github.com/kibotu/Heart-Rate-Ometer
Kotlin课程:
import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.util.Log
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.heartratemeter.*
import net.kibotu.heartrateometer.HeartRateOmeter
import net.kibotu.kalmanrx.jama.Matrix
import net.kibotu.kalmanrx.jkalman.JKalman
class Heartratemeter : AppCompatActivity() {
var subscription: CompositeDisposable? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.heartratemeter)
}
private fun startWithPermissionCheck() {
if (!hasPermission(Manifest.permission.CAMERA)) {
checkPermissions(REQUEST_CAMERA_PERMISSION,
Manifest.permission.CAMERA)
return
}
val kalman = JKalman(2, 1)
// measurement [x]
val m = Matrix(1, 1)
// transitions for x, dx
val tr = arrayOf(doubleArrayOf(1.0, 0.0), doubleArrayOf(0.0, 1.0))
kalman.transition_matrix = Matrix(tr)
// 1s somewhere?
kalman.error_cov_post = kalman.error_cov_post.identity()
val bpmUpdates = HeartRateOmeter()
.withAverageAfterSeconds(3)
.setFingerDetectionListener(this::onFingerChange)
.bpmUpdates(preview)
.subscribe({
if (it.value == 0)
return@subscribe
m.set(0, 0, it.value.toDouble())
// state [x, dx]
val s = kalman.Predict()
// corrected state [x, dx]
val c = kalman.Correct(m)
val bpm = it.copy(value = c.get(0, 0).toInt())
Log.v("HeartRateOmeter", "[onBpm] ${it.value} =>
${bpm.value}")
onBpm(bpm)
}, Throwable::printStackTrace)
subscription?.add(bpmUpdates)
}
@SuppressLint("SetTextI18n")
private fun onBpm(bpm: HeartRateOmeter.Bpm) {
// Log.v("HeartRateOmeter", "[onBpm] $bpm")
label.text = "$bpm bpm"
}
private fun onFingerChange(fingerDetected: Boolean){
finger.text = "$fingerDetected"
}
// region lifecycle
override fun onResume() {
super.onResume()
dispose()
//crashing on subscription = CompositeDisposable()
//subscription = null
subscription = CompositeDisposable()
startWithPermissionCheck()
}
override fun onPause() {
dispose()
super.onPause()
}
private fun dispose() {
if (subscription?.isDisposed == false)
subscription?.dispose()
}
// endregion
// region permission
companion object {
private val REQUEST_CAMERA_PERMISSION = 123
}
private fun checkPermissions(callbackId: Int, vararg permissionsId: String)
{
when {
!hasPermission(*permissionsId) -> try {
ActivityCompat.requestPermissions(this, permissionsId,
callbackId)
} catch (ex: Exception) {
ex.printStackTrace()
}
}
}
private fun hasPermission(vararg permissionsId: String): Boolean {
var hasPermission = true
permissionsId.forEach { permission ->
hasPermission = hasPermission
&& ContextCompat.checkSelfPermission(this, permission) ==
PackageManager.PERMISSION_GRANTED
}
return hasPermission
}
override fun onRequestPermissionsResult(requestCode: Int, permissions:
Array<String>, grantResults: IntArray) {
when (requestCode) {
REQUEST_CAMERA_PERMISSION -> {
if (grantResults.isNotEmpty() && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
startWithPermissionCheck()
}
}
}
}
}
// endregion
的gradle(APP)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.github.dcendents.android-maven'
android {
compileSdkVersion 27
defaultConfig {
applicationId "net.kabuto"
minSdkVersion 16
targetSdkVersion 27
versionCode 4
versionName "4.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.android.gms:play-services-maps:15.0.0'
implementation 'com.google.android.gms:play-services-location:15.0.0'
implementation 'com.google.android.gms:play-services-ads:15.0.0'
implementation 'com.android.support:appcompat-v7:27+'
implementation 'com.android.support:customtabs:27.1.1'
implementation 'com.android.support:recyclerview-v7:27+'
implementation 'com.android.support:animated-vector-drawable:27+'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'com.github.kibotu:Heart-Rate-Ometer:-SNAPSHOT'
implementation ('com.github.kibotu:KalmanRx:1.2.1') { transitive = true }
compileOnly "com.squareup.retrofit2:retrofit:2.3.0"
compileOnly "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:support-media-compat:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.2'
}
repositories {
mavenCentral()
}
apply from: 'https://gist.githubusercontent.com/kibotu/994c9cc65fe623b76b76fedfac74b34b/raw/65ee52482ba65050e015f4b00d8d2fc9172a10e8/javadoc.gradle'
gradle这个(项目)
// Top-level build file where you can add configuration options common to
all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.21'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-
extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我是Kotlin的新人。
答案 0 :(得分:0)
也许添加这些库可能会有所帮助:
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.7'
或尝试将其写出像这样的变量
internal var subscription: CompositeDisposable = CompositeDisposable()