How to assign click events in TabItems? - android

I need to update a list as the user chooses which list to see. I created two items to the TabLayout and I'm trying to get the clik winds but I always get an error.
<com.google.android.material.tabs.TabLayout
android:id="#+id/select_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabContentStart="32dp"
app:tabMode="scrollable">
<com.google.android.material.tabs.TabItem
android:id="#+id/posto1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Posto Shell" />
<com.google.android.material.tabs.TabItem
android:id="#id/posto2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Posto Guanabara" />
</com.google.android.material.tabs.TabLayout>
The error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.tabs.TabItem.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Related

getting an error while updating value for text view android kotlin

here is my layout file which is a part of navigation bar header->
<?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="250dp"
android:background="#color/white">
<ImageView
android:id="#+id/drawerProfileImage"
android:layout_width="163dp"
android:layout_height="157dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#drawable/linus_trovalds"
android:src="#drawable/linus_trovalds"/>
<TextView
android:id="#+id/drawerEmailId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:text="linus-tovalds#linux.com"
android:textColor="#color/gray"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/drawerProfileImage" />
</androidx.constraintlayout.widget.ConstraintLayout>
now in my onCreate method i am trying to change its text but i get an error.
onCreate method code->
//change email id in drawer
val drawerEmailIdValue = "boy#gmail.com"
val drawerEmailId = findViewById<TextView>(R.id.drawerEmailId)
drawerEmailId.text = drawerEmailIdValue //not working for some reason
now here is the error->
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eater/com.example.eater.DishList}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
can someone help with what is going on?
you can't access the drawerEmailId directly as this is not part of activity's xml instead it's inside navigation view.
val navigationView = findViewById<NavigationView>(R.id.nav_view) // nav_view is id for NavigationView
val email = navigationView.getHeaderView(0).findViewById<TextView>(R.id.drawerEmailId)
email.text = "mymail#me.com"
you can do it like above it will get the email TextView from header view.

Android : setting visibilty of a tab in a tablayout not working

I am trying to hide a tab dynamically using databinding.
the code is
<com.google.android.material.tabs.TabItem
android:id="#+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab1" />
<com.google.android.material.tabs.TabItem
android:id="#+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab2" />
<com.google.android.material.tabs.TabItem
android:id="#+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="#{vm.flag ? View.VISIBLE : View.GONE}"
android:text="#string/tab3" />
but this is not working and all three tabs are always visible.
I tried setting it in the code as tablayout.getindex(2).visibility but this gives me NPE.
How do I do this?
You need to modify the adaterList that you pass to the viewpager. Delete the third element and it will disappear
When you need the third tab to appear, simply update the adapterList/viewPager. Check out this post

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)

WrongViewCast error: Unexpected implicit cast to TabLayout: layout tag was Linear Layout

I am trying to copy a previously worked version of a tab layout, however since the library dependencies changed, I get unexpected errors. In my xml, I have:
<com.google.android.material.tabs.TabLayout
android:id="#id/tabLayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/nextButton"
android:layout_marginEnd="15dp"
android:layout_marginStart="20dp"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1st" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2nd" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3rd" />
</com.google.android.material.tabs.TabLayout>
and this is inside a constraint layout. When I try to find this view by id:
TabLayout tabLayout = findViewById(R.id.tabLayout);
I get the error of WrongViewCast. import com.google.android.material.tabs.TabLayout; I import the correct library. What is the correct way of calling findViewById in TabLayout context?
Change android:id="#id/tabLayout" to android:id="#+id/tabLayout" . . .

Having issues writing to EditText in Kotlin

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

Categories

Resources