Logcat within kotlin coroutine? [closed] - android

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
It seems that all logcat logs from within kotlin coroutines are swallowed. Is there a way to see logcat logs that are printed from within a coroutine?

It seems that all logcat logs from within kotlin coroutines are swallowed
Not normally. Normally, they work fine.
I just created a scrap Android Studio Arctic Fox project, using the Empty Activity template. I added implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1' to the dependencies, and I changed MainActivity to be:
package com.commonsware.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
GlobalScope.launch(Dispatchers.Default) {
Log.d("MyApplication", "um, hi!")
}
}
}
(GlobalScope is not a good choice in real work, but I wanted to minimize any hassle)
My Log.d() call worked just fine.
You might try reproducing this experiment and see what you get. Perhaps there is something tied to your environment, your project, or your particular use of coroutines that is causing your problems.

Related

Material Slider OnSliderTouchListener's methods can only be called from within the same library group [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
Implementing BaseOnSliderTouchListener's onStartTrackingTouch and onStopTrackingTouch (documentation) give the lint the following error message :
Error: BaseOnSliderTouchListener.onStartTrackingTouch can only be called from within the same library group (referenced groupId=com.google.android.material from groupId=your-group-id) [RestrictedApi]
I had the same issue. The workaround with #SuppressLint("RestrictedApi") works.
The root cause is that the BaseOnSliderTouchListener class has a library restricted scope and the methods exposed there are not overridden in OnSliderTouchListener.
The issue is tracked in the material-components library here: https://github.com/material-components/material-components-android/issues/2493
It has been fixed in the 1.6.0-alpha02 release of material-components.
Temporary solution:
Add #SuppressLint("RestrictedApi") annotation.
Example:
slider.addOnSliderTouchListener(object : Slider.OnSliderTouchListener {
#SuppressLint("RestrictedApi")
override fun onStartTrackingTouch(slider: Slider) {
[...]
}
#SuppressLint("RestrictedApi")
override fun onStopTrackingTouch(slider: Slider) {
[...]
}
})

I get an error (unresolved reference) in Android Studio when trying to use "new View.OnClickListener" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Im a beginner in Android Studio and have a school project where I have to create an login screen with password and username. When trying to follow some instructions online I get an error even though I have done the same as the instructor. Can you see what I have done wrong?
Example image
Your code is in Kotlin while the video you linked uses Java, so the error indicates that the onClickListener is not following Kotlin syntax properly.
The equivalent in Kotlin is:
logIn.setOnClickListener {
// Do some work here
}
or
logIn.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View?) {
// Do some work here
}
})
Both will behave similarly. See alternative ways here.
// declare
private lateinit var logIn: Button
// cast
logIn = findViewById(R.id.logIn)
// execute the func you want
logIn.setOnClickListener {
executeLogInApi()
}

New to kotlin - textview editing is failing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am currently in a android app development course and am brand new to kotlin. Right now I'm working on building a basic pizza ordering application that once the user makes selections and hits "submit" a textview is edited to show the total price... Except that I cant get the textview to display anything. What am i missing here because i followed the book step-by-step (but it seems to be fairly old) thank you so much!
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onPlaceOrderButtonClicked(view: View) {
var pizzaSizePrice=0.0
var toppingsTotal = 0.0
when {
radioGroup.smallpizza.isChecked -> pizzaSizePrice=5.0
radioGroup.mediumpizza.isChecked -> pizzaSizePrice=7.0
radioGroup.largepizza.isChecked -> pizzaSizePrice=9.0
}
if (OnionsCheckBox.isChecked){toppingsTotal+=1}
if (OlivesCheckBox.isChecked){toppingsTotal+=2}
if (TomatoesCheckBox.isChecked){toppingsTotal+=3}
Totalprice.text=("Total order price= $" + (pizzaSizePrice+toppingsTotal))
}
}
It doesn't look like your code calls onPlaceOrderButtonClicked, try adding this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Calls your function when button is clicked
orderButton.setOnClickListener {
onPlaceOrderButtonClicked()
}
}
This will direct clicks from the button with id orderButton to onPlaceOrderButtonClicked(). Also, remove view: View from that function, it's not being used.
I'm guessing you aren't calling the onPlaceOrderButtonClicked(), there a number of ways to handle this. I'm my guess is your book is using the XML approach because the onPlacedORderButtonClicked() has a view argument. Try including an onClick in the button XML.
<Button
android:id="#+id/btn_orderpizza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLUS"
android:onClick="onPlaceOrderButtonClicked" />

Jetpack Compose: Exception while analyzing expression

I am trying to build simple app using Jetpack Compose.
I followed this documentation, downloaded repository and created my own module.
Code is pretty simple:
import android.app.Activity
import android.os.Bundle
import androidx.compose.Composable
import androidx.ui.core.Text
import androidx.ui.core.setContent
import androidx.ui.material.surface.Card
import androidx.ui.graphics.Color
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApp()
}
}
#Composable
fun MyApp() {
Card(color = Color.Cyan) {
Text("test")
}
}
}
But I noticed that some composable widgets doesnt work and I have following error:
Exception while analyzing expression at (23,9) in
/path/Projects/androidx-master-dev/frameworks/support/ui/compose/src/main/java/app/myown/MainActivity.kt
Where (23,9) references to Card widget
By the way other widgets work, for example I dont have problems with
#Composable
fun MyApp() {
Padding(10.dp) {
Text("test")
}
}
It compiles and runs perfectly.
I got following problem with:
Card
Column
Row
Center
FlexColumn
and I guess many others widgets
I ran into this problem earlier.
There is an implicit need to have import androidx.compose.composer in every Kotlin source file that has #Composable functions. I say "implicit" because Android Studio thinks that it is unnecessary and has a tendency to remove that line (e.g., you ask it to optimize imports). Some #Composable functions can survive without this import, but others cannot.
As I understand it, this is one of those things that will get better as the libraries and tooling evolve, but at the moment, just keep an eye out for that import and add it if it is missing and you are getting weirder-than-normal results.

Kotlin on Android alters library variable instead of defining a new variable

today I started a new Android project with Kotlin support. But as soon as I launch it on my phone, it is disconnected from Wi-Fi (Application has INTERNET and ACCESS_NETWORK_STATE permissons but no socket code yet). Here is the code:
package org.arch.cast
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
internal var dummy = 10
internal var channel = 20
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
After experimenting a bit I noticed that one of my variables wasn't marked as "not used" even though it was not used.
This is the only class the project contains right now, no other service or activity exists so nothing is referring channel variable anywhere in the project. I also added a dummy variable to show that it is marked as not used. I noticed that the problem was its name and it was actually not defining it but altering another variable from a library I didn't import. And since it was related to Wi-Fi, phone was disconnecting.
Sure everything works when I change the variable name but this should not be the solution, it is only a temporary workaround. So the question is, how can I prevent Kotlin from altering the variable and make it actually define it in my class?

Categories

Resources