Hi, I’m trying to create a thread, I wrote this code but when the thread starts the application stop. How can I fix?
package com.example.matteoarcangeli.tesina
import java.io.BufferedInputStream
import java.net.HttpURLConnection
import java.net.URL
class Macchinetta(var cmd: String) : Thread() {
init {
run()
super.start()
}
override fun run() {
val urlConnection = URL("http://192.168.4.1" + '/'.toString() + cmd).openConnection() as HttpURLConnection
try {
val inp = BufferedInputStream(urlConnection.inputStream)
inp.close()
} finally {
urlConnection.disconnect()
}
}
}
Why don’t you just use the thread()
function?
Also, what does it mean that the application stops, does it crash or just closes?
Maybe it prints something that can help understand what exactly is happening?
Sorry, application stop means that it crash
Also in in this way the thread will execute but then application crash
Thread({
val urlConnection = URL("http://192.168.4.1" + '/'.toString() + cmd).openConnection() as HttpURLConnection
try {
val inp = BufferedInputStream(urlConnection.inputStream)
inp.close()
} finally {
urlConnection.disconnect()
}
}).start()
What message is shown when the application crashes?
By the way, do not call run()
from init
, because it should be called automatically from start()
method.
I solved in this way: I create a thread in the mainactivity, but when I swichon accelerometer app crash
class MainActivity : AppCompatActivity(), SensorEventListener {
private lateinit var sensorManager: SensorManager
private lateinit var swAccellerometro: Switch
private lateinit var txtAccellerometo: TextView
private lateinit var car: Car
var background = object : Thread(){
override fun run() {
car = Car(URL("http://192.168.4.1"))
}
}.start()
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
}
@SuppressLint("SetTextI18n")
override fun onSensorChanged(event: SensorEvent?) {
if (swAccellerometro.isChecked) {
val x = event!!.values[0]
val y = event.values[1]
val z = event.values[2]
if (abs(x) > 0.25 || abs(z) > 0.25) {
txtAccellerometo.text = fromHtml("<p><b>x: </b> $x</p><p><b>y: </b> $y</p><p><b>z: </b> $z</p>", FROM_HTML_MODE_LEGACY)
when {
x>0.25 -> car.muovi("right")
/* (x < 0.25 && x > -0.25) || (x > -0.7 && z < -0.25) -> car.muovi("stop")
x > 0.25 -> car.muovi("right")
x < -0.25 -> car.muovi("left")
z < -0.7 -> car.muovi("forward")
z > -0.25 -> car.muovi("backward")*/
}
}
} else
txtAccellerometo.text = "Accellerometro disabilitato"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL)
swAccellerometro = findViewById(swAcc)
txtAccellerometo = findViewById(R.id.textView)
findViewById<View>(R.id.btnAvanti).setOnClickListener {
car.muovi("forward")
}
findViewById<View>(R.id.btnIndietro).setOnClickListener {
car.muovi("backward")
}
findViewById<View>(R.id.btnDestra).setOnClickListener {
car.muovi("right")
}
findViewById<View>(R.id.btnSinistra).setOnClickListener {
car.muovi("left")
}
findViewById<View>(R.id.btnStop).setOnClickListener {
car.muovi("stop")
}
}
}
class Car(private var url:URL) {
fun muovi(cmd: String) {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
val urlConnection = URL(url.toString() + '/' + cmd).openConnection() as HttpURLConnection
try {
val inp = BufferedInputStream(urlConnection.inputStream)
inp.close()
} catch (e: Exception) {
e.printStackTrace()
} finally {
urlConnection.disconnect()
}
}
}