Having issues writing to EditText in Kotlin - android

So I think I'm having some scoping issues.
I'm trying to use the function setZipEdit to set the R.id.zipHolder editText in my activity. I'm just having issues in setting up the code to be able to do this. Below is what I have which isn't currently working. mZipHold is coming back as not initialized which I though it was in the override
UPDATED
class SecondActivity : AppCompatActivity() {
lateinit var townInfo:JsonArray<JsonObject>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity2)
SET_TOWN.setOnClickListener {v -> setTowns()}
GET_TOWNS.setOnClickListener {v -> retrieveTowns()}
}
fun setZipEdit(zipEdit:String){
Log.d("SZE",zipEdit)
zipHolder.setText(zipEdit)
//mZipHold.setText(zipEdit,TextView.BufferType.EDITABLE)
}
java.lang.NullPointerException: Attempt to invoke virtual method
'android.view.Window$Callback android.view.Window.getCallback()' on a
null object reference
at
android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:117)
at
android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:149)
at
android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:29)
at
android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:54)
at
android.support.v7.app.AppCompatDelegateImplV23.(AppCompatDelegateImplV23.java:31)
at
android.support.v7.app.AppCompatDelegateImplN.(AppCompatDelegateImplN.java:31)
at
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
at
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at
android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at
android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at
com.example.sdfsdf.listview.SecondActivity._$_findCachedViewById(SecondActivity.kt:0)
at
com.example.sdfsdf.listview.SecondActivity.setZipEdit(SecondActivity.kt:44)
at
com.example.sdfsdf.listview.SecondActivity$MyCustomAdapter$onCreateViewHolder$1.onClick(SecondActivity.kt:124)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:background="#FFFFFF"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="25dp">
<ImageView
android:id="#+id/returnimage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
android:contentDescription="logoforZip"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_new_icon_plain" />
<EditText
android:id="#+id/zipHolder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="number"
android:text="ZIP HERE"
android:textAlignment="center"
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/returnimage"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/SET_TOWN"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:text="SET TOWN"
app:layout_constraintEnd_toEndOf="#+id/zipHolder"
app:layout_constraintStart_toStartOf="#+id/zipHolder"
app:layout_constraintTop_toBottomOf="#+id/zipHolder" />
<Button
android:id="#+id/GET_TOWNS"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="8dp"
android:text="LIST NEARBY TOWNS"
app:layout_constraintEnd_toEndOf="#+id/SET_TOWN"
app:layout_constraintStart_toStartOf="#+id/SET_TOWN"
app:layout_constraintTop_toBottomOf="#+id/SET_TOWN" />
<View
android:id="#+id/splitbar"
android:layout_width="fill_parent"
android:layout_height="6dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:background="#c0c0c0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/GET_TOWNS" />
<android.support.v7.widget.RecyclerView
android:id="#+id/zipList"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#222222"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/splitbar" />
</android.support.constraint.ConstraintLayout>

Ok, But using kotlinextension is a better way to do this, I think you need to do 2 things may solve your problem
first
import kotlinx.android.synthetic.main.activity_main2.*
I think which you have already done
second instead of initializing your Editext by findViewById direct use as
fun setZipEdit(zipEdit:String){
Log.d("SZE",zipEdit)
Log.d("mzh",mZipHold.toString())
zipHolder.setText(zipEdit)
//mZipHold.setText(zipEdit,TextView.BufferType.EDITABLE)
}
you can access your views by only use their id. Let try this and let me know if works

Related

On the Java side, I get Famous error: on a null object object In the event that definition conditions "findVIewById" are correct

On the Java side, I get Famous error: on a null object object In the event that definition conditions "findVIewById" are correct .
It is interesting that textView With id :#+id/txt_detail_pPrice is identified but #+id/txt_detail_Price NO and throw :on a null object
why findVIewById does not work just for #+id/txt_detail_Price and "price" in java side become
null??
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_details)
val imgProduct=findViewById< MyImageView >(R.id.img_details_showpicProduct)
val txt_title=findViewById<TextView >(R.id.txt_detail_showTitle)
val waranty=findViewById< TextView >(R.id.txt_details_waranty)
val pPrice=findViewById< TextView >(R.id.txt_detail_pPrice)
val price=findViewById<TextView>(R.id.txt_detail_Price)
val color=findViewById< TextView >(R.id.txt_details_color)
val detailIntroduction=findViewById<TextView>(R.id.txt_details_introduction)
val ratingBar=findViewById<RatingBar>(R.id.details_ratingBar_showratingProduct)
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".Details.DetailsActivity">
<com.google.android.material.appbar.AppBarLayout...>
<androidx.core.widget.NestedScrollView...>
<RelativeLayout
android:layout_width="match_parent"
android:layout_gravity="bottom"
android:background="#color/purple_500"
android:layout_height="75dp">
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:backgroundTint="#color/greenIcon"
android:text="افزودن به سبد خرید"
android:textColor="#color/white" />
<TextView
android:id="#+id/txt_detail_pPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="10000000میلیون"
android:textColor="#color/teal_700" />
<TextView
android:id="#+id/txt_detail_Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="20000000میلیون"
android:textColor="#color/black" />
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
If you are quite certain the corresponding view that it can't find is in the layout you passed to setContentView, I think the most likely cause for this issue is that you have multiple versions of this same layout for different screen configurations, and forgot to include a view with this ID in all of them. When the screen is in a configuration where the view is not included, it cannot be found.

Android studios app Login failed and app crashes [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 2 years ago.
Improve this question
I am getting the following errors when I try login to my app using firebase as well as the app crashing.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.newsapp, PID: 31052
java.lang.NullPointerException: usernameInput must not be null
at com.example.newsapp.LoginActivity$login$1.onClick(LoginActivity.kt:31)
at android.view.View.performClick(View.java:6294)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
my activity_login.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#drawable/gradient"
tools:context=".LoginActivity">
<ImageView
android:id="#+id/imageView3"
android:layout_width="201dp"
android:layout_height="195dp"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:layout_marginEnd="146dp"
android:layout_marginBottom="500dp"
app:srcCompat="#drawable/newslogo" />
<EditText
android:id="#+id/username"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="105dp"
android:layout_marginBottom="432dp"
android:ems="10"
android:inputType="textPersonName"
android:drawableLeft="#drawable/ic_action_user"
android:text="Username"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF" />
<EditText
android:id="#+id/password"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="105dp"
android:layout_marginBottom="345dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:drawableLeft="#drawable/ic_action_pass"
android:textColorHint="#FFFFFF" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/registerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="163dp"
android:layout_marginBottom="160dp"
android:text="#string/register_here"
android:textColor="#FFFFFF"
android:textSize="14dp" />
<Button
android:id="#+id/loginButton"
android:layout_width="196dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="108dp"
android:layout_marginEnd="107dp"
android:layout_marginBottom="237dp"
android:backgroundTint="#color/colorAccent"
android:text="#string/Login" />
</RelativeLayout>
does anyone know what i'm doing wrong and how to fix it. Any help is appreciated. Also does anyone know how to display profile details onto an fragment. The details I want is from the firebase database.
Thanks
For the userInput & passwordInput, Your xml ids are not the same as you used in findViewById
You need to make sure they are the same.
You can use the below findViewById instead in login activity that has Ids given from the layout
....
private fun login(){
val loginButton = findViewById<Button>(R.id.loginButton)
val usernameInput = findViewById<EditText>(R.id.username)
val passwordInput = findViewById<EditText>(R.id.password)
//...

Validation using binding adapter

i need some clearance in my concept of understanding the binding adapter implementation.
I am designing a login form with two input fields and a button. I'm trying to use binding adapter to validate those fields using text watcher (which is working fine).
But i'm lost that how can i validate those fields on click of the button.
i'm trying to achieve a clean architecture and remove all kind of dependency between view and ViewModel.
login_activity.xml
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="authModel"
type="com.jpm.ui.auth.AuthViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorSurface"
tools:context=".ui.auth.LoginActivty">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/logo"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="60dp"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/jpm_logo" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/layoutLoginId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="40dp"
android:hint="#string/login_id_hint"
app:layout_constraintTop_toBottomOf="#id/logo"
app:startIconDrawable="#drawable/ic_user"
nullCheck="#{#string/nullError}">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/inputLoginId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#={authModel.inputLoginId}" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/layoutPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="40dp"
android:hint="#string/password"
app:layout_constraintTop_toBottomOf="#id/layoutLoginId"
app:passwordToggleEnabled="true"
app:startIconDrawable="#drawable/ic_locked">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/inputPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#={authModel.inputPassword}" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/btnSignIn"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:background="#drawable/bg_primary_rounded"
android:onClick="#{authModel::authClickListener}"
android:paddingStart="40dp"
android:paddingEnd="40dp"
android:text="#string/sign_in"
android:textColor="#color/textSecondary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/layoutPassword" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/labelCreateAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/don_t_have_an_account"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#id/txtCreateAccount"
app:layout_constraintTop_toBottomOf="#id/btnSignIn" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/txtCreateAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="20dp"
android:onClick="#{authModel::authClickListener}"
android:text="#string/create_account"
android:textColor="#color/colorPrimary"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toRightOf="#id/labelCreateAccount"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/btnSignIn" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/txtSkip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:onClick="#{authModel::authClickListener}"
android:text="#string/skip"
android:textColor="#color/textGrey"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/txtCreateAccount" />
</androidx.constraintlayout.widget.ConstraintLayout>
ValidationUtils.class
import android.util.Log
import androidx.core.widget.doAfterTextChanged
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.BindingAdapter
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.google.common.base.Predicates
import com.jpm.R
object ValidationUtils : BaseObservable() {
const val TAG = "VALIDATION"
#BindingAdapter("nullCheck")
#JvmStatic
fun setError(inputLayout: TextInputLayout, errorMsg: String) {
inputLayout.editText?.doAfterTextChanged {
if (it.isNullOrEmpty()) {
inputLayout.isErrorEnabled = true
inputLayout.error = errorMsg
} else {
inputLayout.isErrorEnabled = false
inputLayout.error = null
}
}
}
}
Please help with some suggestion or what can i implement to achieve the validation on button click.
**PS - On Validation on button click setError should be applied on EditText as well **
This is what I'd use in your case:
When your ViewModel's authClickListener() is called, you check/validate every input that is bound with two-way databinding, in your case inputLoginId and the password.
Additionally you set up two error observables, with the type String?, tie does to the error of the respective TextInputLayout and just modify the error messages according to your validation logic

java.lang.NullPointerException on Button in android

This code was working fine before but now i get null pointer exception on the button
import kotlinx.android.synthetic.main.activity_splash.*
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
button_splash_getting_started.setOnClickListener {
val intent = Intent(this#SplashActivity, RegisterActivity::class.java)
intent.putExtra("FROM_SPLASH_ACTIVITY",true)
startActivity(intent)
}
textView_splash_already_have_account.setOnClickListener {
val loginIntent = Intent(this#SplashActivity, LoginActivity::class.java)
loginIntent.putExtra("FROM_SPLASH_ACTIVITY",true)
startActivity(loginIntent)
}
}
error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
and here is logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edubaba.howApp/com.test.ui.SplashActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.edubaba.howApp.ui.splash.SplashActivity.onCreate(SplashActivity.kt:35)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
here is the xml file
<?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:id="#+id/activity_splash_root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="customsnackbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/activity_splash_constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView_splash_background"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/how_background" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/activity_splash_skip_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:padding="20dp"
android:text="#string/skip"
android:textColor="#color/white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/activity_splash_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/progressBarWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_splash_getting_started"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="16dp"
android:background="#drawable/background_button_getting_started"
android:text="#string/get_started"
android:textColor="#ec975d"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/textView_splash_already_have_account"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="798dp" />
<TextView
android:id="#+id/textView_splash_already_have_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="128dp"
android:text="#string/already_signed"
android:textColor="#color/white"
android:translationZ="1dp"
app:layout_constraintBottom_toBottomOf="#+id/activity_splash_constraintLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Can anyone tell what could have possibly gone wrong?
This id button_splash_getting_started Doesn't exist on your Xml that's why you getting error.
Replace this id button_splash_getting_started with this button_splash_getting_started1.
That's it .
I think I found the problem. In onCreate() method you have:
setContentView(R.layout.activity_splash)
But your .xml file have id:
android:id="#+id/activity_splash_root_layout"
So, I think you need replace to setContentView() right id="".
For example:
setContentView(R.id.activity_splash_root_layout)

Android-kotlin-binding doesn't update the properties [duplicate]

This question already has an answer here:
Kotlin android. binding not update data
(1 answer)
Closed 5 years ago.
My problem is the binding is work, but not correctly! When I type new text in textfield and then try to get data from this textfield with help binding, I see old data. I tried to find the mistake, but I couldn't.
My activity_fblogin.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey_main"
android:descendantFocusability="beforeDescendants"
android:fitsSystemWindows="true"
android:focusableInTouchMode="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context="com.example.darkt.makeyouself.activities.FBLogin"
tools:ignore="MissingConstraints">
<EditText
android:id="#+id/userName"
android:layout_width="220dp"
android:layout_height="43dp"
android:ems="10"
android:inputType="textPersonName"
android:text="test01"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Login"/>
<EditText
android:id="#+id/userPass"
android:layout_width="220dp"
android:layout_height="45dp"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="textPassword"
android:text="123456"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/userName"
tools:layout_editor_absoluteX="74dp"
tools:layout_editor_absoluteY="245dp"
tools:text="Password"/>
<Button
android:id="#+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Login"
android:textColor="#color/green_main"
app:layout_constraintEnd_toEndOf="#+id/userPass"
app:layout_constraintTop_toBottomOf="#+id/userPass" />
</android.support.constraint.ConstraintLayout>
</layout>
It is my kotlin class for fblogin_activity. I was trying to fixed this problem, but ... Give me idea please!
package com.example.darkt.makeyouself.activities
class FBLogin : AppCompatActivity() {
private var binding: ActivityFbloginBinding? = null
private var auth: FirebaseAuth? = null
private var dbHelper: FirebaseHelper? =null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_fblogin)
auth = FirebaseAuth.getInstance()
dbHelper = FirebaseHelper()
setContentView(R.layout.activity_fblogin)
binding?.executePendingBindings()
signIn.setOnClickListener{_ -> showSignInActivity()}
}
private fun loginToSystem() {
val email = binding?.userName?.text.toString().trim()
val password = binding?.userPass?.text.toString()
binding?.executePendingBindings()
Toast.makeText(this, email, Toast.LENGTH_SHORT).show()
Toast.makeText(this, password, Toast.LENGTH_SHORT).show()
}
}
Example mistake
There is nothing in your layout xml that actually uses databinding.
first in your tag, you will need to define an object to reference for data binding. for example:
<data>
<variable name="user" type="com.myapp.User"/>
</data>
further down in the username/password field you would need to reference this user for the text field
<EditText
android:id="#+id/userName"
android:layout_width="220dp"
android:layout_height="43dp"
android:ems="10"
android:inputType="textPersonName"
android:text="#={user.username}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<EditText
android:id="#+id/userPass"
android:layout_width="220dp"
android:layout_height="45dp"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="textPassword"
android:text="#={user.password}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/userName"
tools:layout_editor_absoluteX="74dp"
tools:layout_editor_absoluteY="245dp"/>
Note the #={user.username} in the text field. For every property of the class user, you will need to specify a setter/getter. the #= means, update the underlying object when this value changes.
before your "executePendingBindings" you will need to do something like this:
val user = User()
binding.setVariable(BR.user, user)
binding.executePendingBindings()
then you should be good to go.

Categories

Resources