以下のプログラムをMainActivity.ktで行っているのですが、setContentView(R.layout.activity_main)のところでactivity_mainを参照できていないようです。
どうにかして参照したいのですが、ご教授いただければ幸いです。
package BVP3
import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.Typeface
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.wearable.activity.WearableActivity
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
class MainActivity : WearableActivity(), SensorEventListener {
private val TAG = MainActivity::class.java.name
private var mSensorManager: SensorManager? = null
var hb = 100.0f
private var textView: TextView? = null
private var heartTextView: TextView? = null
var backGround: View? = null
var isDisp = true
private val loopEngine: LoopEngine = LoopEngine()
var imageView: ImageView? = null
var set = false
protected override fun onStart() {
super.onStart()
}
protected override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main); setAmbientEnabled() mSensorManager = ContextCompat.getSystemService(SENSOR_SERVICE) as SensorManager? textView = findViewById(R.id.text) as TextView? heartTextView = findViewById(R.id.text_heart) as TextView? backGround = findViewById(R.id.View) as View? textView!!.textSize = 20.0f heartTextView!!.textSize = 0.0f loopEngine.start() //heartTextView.setTextColor(Color.argb(80, 67, 135, 233)); textView!!.setTextColor(Color.argb(255, 140, 140, 140)) textView!!.setTypeface(Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD)) } private fun createParam(w: Int, h: Int): LinearLayout.LayoutParams { return LinearLayout.LayoutParams(w, h) } protected override fun onResume() { super.onResume() val sensor = mSensorManager!!.getDefaultSensor(Sensor.TYPE_HEART_RATE) mSensorManager!!.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL) } protected override fun onPause() { super.onPause() mSensorManager!!.unregisterListener(this) } override fun onSensorChanged(event: SensorEvent) { //ここで変数宣言すると,起動中は破棄されずメモリリークする if (set == false) textView!!.textSize = 60.0f if (event.sensor.type == Sensor.TYPE_HEART_RATE) { hb = event.values[0] textView!!.text = "" + hb.toInt() set = true } } override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) { Log.d(TAG, "onAccuracyChanged!!") } fun update() { if (set) { if (isDisp) { backGround!!.setBackgroundColor(Color.argb(80, 231, 232, 226)) //heartTextView.setTextSize(100.0f); textView!!.textSize = 60.0f val img = findViewById(R.id.imageView) as ImageView val res: Resources = getResources() val bitmap = BitmapFactory.decodeResource(res, R.drawable.heart) // bitmapの画像を250*250で作成する val bitmap2 = Bitmap.createScaledBitmap(bitmap, 250, 250, false) img.setImageBitmap(bitmap2) } else { backGround!!.setBackgroundColor(Color.argb(10, 231, 232, 226)) //heartTextView.setTextSize(800.0f); textView!!.textSize = 70.0f val img = findViewById(R.id.imageView) as ImageView val res: Resources = getResources() val bitmap = BitmapFactory.decodeResource(res, R.drawable.heart) // bitmapの画像を300*300で作成する val bitmap2 = Bitmap.createScaledBitmap(bitmap, 300, 300, false) img.setImageBitmap(bitmap2) } } isDisp = !isDisp } //一定時間後にupdateを呼ぶためのオブジェクト internal inner class LoopEngine : Handler() { private var isUpdate = false fun start() { isUpdate = true handleMessage(Message()) } fun stop() { isUpdate = false } override fun handleMessage(msg: Message) { this.removeMessages(0) //既存のメッセージは削除 if (isUpdate) { update() //自信が発したメッセージを取得してupdateを実行 sendMessageDelayed(obtainMessage(0), (60 / hb * 1000).toLong()) //鼓動の間隔でメッセージを出力 } } }
}
あなたの回答
tips
プレビュー