Android Studio > Kotlin > Toast error > E/GraphicExt - android

I'm running a simple counter (counts how much a button is pressed) on my physical device instead of android emulator and everything seems fine except the toast message seems to be faulty, it shows the message, but when i tap the button that triggers the Toast multiple times, the Toast message has a delay on showing between one tap and another.
This is the Toast:
Toast.makeText(this,"Works", Toast.LENGTH_SHORT).show()
Also the android studio shows this error:
E/GraphicExt: GraphicExtModuleLoader::CreateGraphicExtInstance false
My device is a Xiaomi Redmi Note 8 Pro, running android 10.
Error image
EDIT:
This is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First app!"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginLeft="162dp"
android:layout_marginTop="255dp"
android:layout_marginEnd="162dp"
android:layout_marginRight="162dp"
android:layout_marginBottom="42dp"
android:text="Click me!"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
and this is the MainActivity:
package com.example.helloworld
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var timesClicked = 0
button.setOnClickListener {
timesClicked += 1
textView.text = timesClicked.toString()
Toast.makeText(this#MainActivity, "Button works", Toast.LENGTH_SHORT).show()
}
}
}

Related

getting an error when using toDouble() and toIntOrNull()

I'm writing a simple tip calculator app in android studio ( which is one of the programs in the https://developer.android.com/courses/android-basics-kotlin/course course ).
But I'm getting an unexpected error, unresolved reference: toDouble, when I try to covert a string to a double. Even after pasting the solution code from the website into my application, the error won't go away. I also tried first converting the string to an int using toInt function but I get the same error.
Here is the code inside MainActivity.kt.
package com.example.tiptime
import java.text.NumberFormat
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.tiptime.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.calculateButton.setOnClickListener{ calculateTip() }
}
fun calculateTip() {
val stringInTextField = binding.costOfService.text.toString()
val cost = stringInTextField.toDouble()
}
}
Here is my code in activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity"
tools:ignore="UseSwitchCompatOrMaterialXml">
<EditText
android:id="#+id/cost_of_service"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:hint="#string/cost_of_service"
android:inputType="numberDecimal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:autofillHints="#string/cost_of_service" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/how_was_the_service"
android:id="#+id/service_question"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cost_of_service"/>
<RadioGroup
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/tip_options"
android:checkedButton="#+id/option_twenty_percent"
app:layout_constraintTop_toBottomOf="#id/service_question"
app:layout_constraintStart_toStartOf="parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option_twenty_percent"
android:text="#string/amazing_20"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option_eighteen_percent"
android:text="#string/great_18"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/option_fifteen_percent"
android:text="#string/ok_15"/>
</RadioGroup>
<Switch
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/round_up_switch"
android:text="#string/round_up_tip"
android:checked="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tip_options" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:id="#+id/calculate_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/round_up_switch"
/>
<TextView
android:id="#+id/tip_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tip_amount"
app:layout_constraintTop_toBottomOf="#id/calculate_button"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
And here is the screenshot of the error
Any help is appreciated!!
Seems like you do not have the latest kotlin support .
Update the Kotlin version to the latest one from :
File ->> Setting -->> Language & Frameworks -->> Kotlin ...
Try after removing
import java.text.NumberFormat
comment
fun calculateTip() {
val stringInTextField = binding.costOfService.text.toString()
val cost = stringInTextField.toDouble()
}
build & clean
uncomment the code in above method you will get correct import
Use
val cost = Double.parseDouble(stringInTextField)

editableText is not working in android studio

I want to take Input from user(their name) with editableText just like the below video(watch at 8:24). But, I am not able to take input because android studio is not recognizing "editableText". Am i missing something?
Android Development Tutorial Video at 8:24
Error ->>
Unresolved reference: nameInput [In the below MainActivity.kt]
MainActivity.kt
package com.example.firstapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun createBirthdayCard(view: View){
val name = nameInput.**editableText**.toString() <-- [error is in this line]
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="140dp"
android:text="Enter Name"
android:textColor="#000000"
android:textSize="50sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:textAlignment="center" />
<EditText
android:id="#+id/nameInput"
android:layout_width="280dp"
android:layout_height="54dp"
android:layout_marginTop="64dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
tools:textAlignment="center" />
<Button
android:id="#+id/createBirthdayCard"
android:layout_width="307dp"
android:layout_height="81dp"
android:layout_marginTop="144dp"
android:maxLines="1"
android:text="Create Birthday Card"
android:textSize="19sp"
android:onClick="createBirthdayCard"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/nameInput"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
:) :) Thanks for spending your valuable time to help me :) :)
Android kotlin extensions removed at Android studio 4.1V.
I recommend use the ViewBinding or DataBinding.
This is a good reference.
https://android-developers.googleblog.com/2020/11/the-future-of-kotlin-android-extensions.html
ViewBinding : https://developer.android.com/topic/libraries/view-binding?hl=ko
DataBinding : https://developer.android.com/topic/libraries/data-binding
As Android kotlin extensions is deprecated and removed you may use
View binding, Data binding as suggested by Seungho Kang
OR you can declare variable as shown below.
fun createBirthdayCard(view: View){
val editText=findViewById<EditText>(R.id.nameInput)
val name = editText.editableText.toString()
}

Android Studio: Elements doesn't react to interactions (but only in Emulator)

I have made a simple Android-UI. Actually I started with the spinner. The button I added only for counterchecking, after the spinner didn't work.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="#+id/spinner2"
android:layout_width="409dp"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:entries="#array/months"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="156dp"
android:text="Test ..."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinner2" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.mizech.playground
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
testButton.setOnClickListener {
Toast.makeText(this, "Testing 123 ...", Toast.LENGTH_LONG).show();
}
}
}
The strings.xml (although I think it isn't relevant):
<resources>
<string name="app_name">Playground</string>
<string-array name="months">
<item>January</item>
<item>February</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
</string-array>
</resources>
The emulator is like frozen. Neither the spinner nor the button react to any interaction.
On a physical device all is fine. The spinner expands it's list when clicked. The button works as well.
What can be the cause, that the emulator behaves suddenly that way? How can it be fixed?
I have used this virtual device for some time. The described problems came out of the sudden.
Maybe your minsdk and maxsdk versions are not matching with the emulators minsdk and maxsdk.

ID showing up as Unresolved reference in MainActivity.kt

I have looked through many examples but i really cant figure this out. I literally just started working with Kotlin and android studio. Any help would be appreciated
Hey is my code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/login_background"
android:gravity="center"
android:padding="30dp"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/Enter_No"
android:textSize="22sp"
android:inputType="phone"
/>
<Button
android:id="#+id/btnLogin"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#fff"
android:textColor="#1495d7"
android:text="#string/Sign_in" />
<Button
android:id="#+id/FrgtPass"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="#string/Frgt_pass_prmt"
android:textAllCaps="false" />
</LinearLayout>
MainActivity.kt
package com.example.hackapp1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnLogin.setOnClickListener{}
}
}
Maybe, you did not apply kotlin-android-extension plugin.
It was applied automatically, but not now.
kotlin-android-extension has some issues. It will be deprecated in near future.
See details here.
Instead of kotlin-android-extension ViewBinding and DataBinding is recommended.
You can simply enable like following:
android {
...
viewBinding { enabled = true }
dataBinding { enabled = true }
}
See below links for details.
DataBinding
ViewBinding

How to reference a timerPicker in android studio using kotlin

When this program runs on the emulator it works, however, when I run it on my phone the timePicker is referenced as null.
An emulator running API 26 can run it fine, however, at API 28 it does not work. So I think It might be something with API 28 (which also is what my phone runs)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val picker = findViewById<TimePicker>(R.id.timePicker)
val t = picker == null // Is true on phone and is false on emulator
Toast.makeText(this#MainActivity, t.toString(), Toast.LENGTH_LONG).show()
//picker.setIs24HourView(true)
val activity = findViewById<EditText>(R.id.activityName)
val mCallApiButton = findViewById<Button>(R.id.submit)
mCallApiButton?.setOnClickListener{
val time = GetTime(picker)
PostActivity(time, activity.text.toString()) // Function that does other stuff
}
}
}
I need to reference the timePicker so that I can set it to 24H.
Here is a look at the layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" app:layout_constraintTop_toTopOf="parent">
<TimePicker android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="clock"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/activityName"
android:singleLine="true" android:hint="Activity" android:textSize="18sp"
android:textAlignment="center" android:autofillHints="Activity"/>
<Button
android:text="Submit"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/submit"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshot of the emulator: (Which is how it should look)
Screenshot of the phone:
This is because I use API 28 and not 27 and under. The answer can found here:
https://github.com/xamarin/Xamarin.Forms/issues/5159

Categories

Resources