Reputation: 145
*when the emulator is turned on, it crashes when trying to change parameters
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
if (editText.text.toString().isEmpty()) {
status.text = "error"
}
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
try { editText2.setText("${editText.toString().toFloat() * 39370}")
}
catch (f: NumberFormatException){}
status.text = "error"
editText2.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
try {
editText.setText("${editText2.text.toString().toFloat() / 39370}")
}catch (e: NumberFormatException){
status.text = "error"
}
}
override fun afterTextChanged(editable: Editable) {
if(editText2.text.toString().isEmpty()) {
status.text = "error"
}
}
})
}
})
}
LOGCAT
2021-08-07 15:14:31.145 552-10994/system_process E/ActivityManager: ANR in com.example.company.myapplication (com.example.company.myapplication/.MainActivity)
PID: 10958
Reason: Input dispatching timed out (1a128e0 com.example.company.myapplication/com.example.company.myapplication.MainActivity (server) is not responding. Waited 5000ms for KeyEvent(deviceId=0, source=0x00000301, displayId=-1, action=DOWN, flags=0x00000008, keyCode=67, scanCode=14, metaState=0x00000000, repeatCount=0), policyFlags=0x62000000)
Parent: com.example.company.myapplication/.MainActivity
Load: 0.35 / 0.29 / 0.28
----- Output from /proc/pressure/memory -----
some avg10=0.00 avg60=0.00 avg300=0.00 total=5379069
full avg10=0.00 avg60=0.00 avg300=0.00 total=859915
----- End output from /proc/pressure/memory -----
CPU usage from 53152ms to 0ms ago (2021-08-07 12:13:35.741 to 2021-08-07 12:14:28.893):
2.7% 308/[email protected]: 0% user + 2.7% kernel
2.3% 384/surfaceflinger: 0% user + 2.2% kernel / faults: 42 minor
2.2% 330/[email protected]: 0% user + 2.2% kernel
2.2% 552/system_server: 0.3% user + 1.8% kernel / faults: 4538 minor
1.1% 9469/com.google.android.inputmethod.latin: 0.1% user + 0.9% kernel / faults: 3447 minor
0.7% 813/com.android.systemui: 0% user + 0.7% kernel / faults: 87 minor
0.5% 364/[email protected]: 0% user + 0.5% kernel
0.1% 483/llkd: 0% user + 0% kernel
0.3% 1532/com.google.android.gms: 0.1% user + 0.1% kernel / faults: 3361 minor
0.3% 404/adbd: 0% user + 0.2% kernel / faults: 286 minor
0.2% 305/android.hardware.audio.service.ranchu: 0% user + 0.2% kernel / faults: 3 minor
0.2% 169/logd: 0% user + 0.2% kernel / faults: 6 minor
0.1% 329/[email protected]: 0% user + 0.1% kernel
0.1% 388/audioserver: 0% user + 0.1% kernel / faults: 3 minor
0.1% 1261/com.google.android.gms.persistent: 0% user + 0.1% kernel / faults: 482 minor
0.1% 3154/com.google.android.googlequicksearchbox:search: 0% user + 0.1% kernel / faults: 2413 minor 2 major
0.1% 10/rcu_preempt: 0% user + 0.1% kernel
0% 392/logcat: 0% user + 0% kernel
0% 1156/com.android.phone: 0% user + 0% kernel / faults: 58 minor
0% 26/ksoftirqd/3: 0% user + 0% kernel
0% 8960/logcat: 0% user + 0% kernel
0% 16/ksoftirqd/1: 0% user + 0% kernel
0% 421/traced_probes: 0% user + 0% kernel / faults: 1 minor
0% 10421/kworker/u8:0-cfg80211: 0% user + 0% kernel
0% 10627/kworker/u8:1-phy0: 0% user + 0% kernel
0% 171/servicemanager: 0% user + 0% kernel
0% 1077/com.google.android.apps.nexuslauncher: 0% user + 0% kernel / faults: 46 minor
0% 1/init: 0% user + 0% kernel
0% 9/ksoftirqd/0: 0% user + 0% kernel
0% 21/ksoftirqd/2: 0% user + 0% kernel
0% 170/lmkd: 0% user + 0% kernel
0% 294/statsd: 0% user + 0% kernel / faults: 5 minor
0% 296/zygote: 0% user + 0% kernel / faults: 168 minor
0% 370/[email protected]: 0% user + 0% kernel
0% 443/iorapd: 0% user + 0% kernel / faults: 26 minor
0% 454/wificond: 0% user + 0% kernel
0% 1018/[email protected]: 0% user + 0% kernel
0% 1056/com.android.networkstack.process: 0% user + 0% kernel / faults: 24 minor
0% 7971/kworker/3:0-mm_percpu_wq: 0% user + 0% kernel
+0% 10958/com.example.company.myapplication: 0% user + 0% kernel
+0% 10983/kworker/0:2-mm_percpu_wq: 0% user + 0% kernel
8.5% TOTAL: 4.1% user + 4.3% kernel + 0% iowait + 0% softirq
CPU usage from 20ms to 384ms later (2021-08-07 12:14:28.913 to 2021-08-07 12:14:29.276):
124% 10958/com.example.company.myapplication: 102% user + 22% kernel / faults: 1789 minor
97% 10958/y.myapplication: 97% user + 0% kernel
26% 10972/HeapTaskDaemon: 4.4% user + 22% kernel
33% 552/system_server: 0% user + 33% kernel / faults: 281 minor
29% 10994/AnrConsumer: 0% user + 29% kernel
3.1% 308/[email protected]: 0% user + 3.1% kernel
3.1% 1328/[email protected]: 0% user + 3.1% kernel
2021-08-07 15:14:31.145 552-10994/system_process E/ActivityManager: 3.1% 330/[email protected]: 0% user + 3.1% kernel
3.1% 359/: 0% user + 3.1% kernel
44% TOTAL: 23% user + 21% kernel
Upvotes: 0
Views: 259
Reputation: 19622
You're getting an Application Not Responding crash, which means your code is blocking and taking too long to do something. It can be a sign you're in a neverending loop, which is probably what's happening here.
You're setting two TextWatcher
s here, one on editText
and one on editText2
. The onTextChanged
callback fires when... the text changes obviously! When the text changes in editText
, that method runs and does this:
editText2.setText("${editText.toString().toFloat() * 39370}")
It's setting the text on editText2
. So its onTextChanged
fires, and does this:
editText.setText("${editText2.text.toString().toFloat() / 39370}")
See the problem here? Each time the text changes in one, it changes the text in the other, which changes the text in the first one again, which changes... you get the idea. It never ends, and your app hangs
Upvotes: 0
Reputation: 156
edittask.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
val inputText: String = edittask.getText().toString()
if (inputText.length > 0) {
colordot.setText(Html.fromHtml("•"))
} else {
layout.setVisibility(View.GONE)
addtask.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.color.gray)))
}
}
override fun afterTextChanged(editable: Editable) {
val inputText: String = edittask.getText().toString()
if (inputText.length > 0) {
addtask.setEnabled(true)
addtask.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.color.bg)))
} else {
addtask.setEnabled(false)
}
}
})
Upvotes: 1