Reputation: 553
I'm trying to manipulate elements from xml of a fragment, but all tries I did, result in some error. When I use root!!.qr_code.visibility = View.INVISIBLE, I got this:
What is wrong with my elements reference?
FATAL EXCEPTION: main Process: com.example.test, PID: 26445 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.myapplication.MainActivity}: kotlin.KotlinNullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6810) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) Caused by: kotlin.KotlinNullPointerException at com.example.myapplication.ui.home.HomeFragment.setElementVisibility(HomeFragment.kt:49) at com.example.myapplication.MainActivity.onCreate(MainActivity.kt:72)
My HomeFragment: (called from onCreate in MainActivity)
package com.example.myapplication.ui.home
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.example.myapplication.R
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_home.view.*
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
private var root: View? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreate(savedInstanceState);
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel::class.java)
root = inflater.inflate(R.layout.fragment_home, container, false)
//The findViewById appears as unresolved reference.
qrcode = findViewById(R.id.qr_code) as LinearLayout
return root
}
fun setElementVisibility(permissionData: Boolean, serialData: Boolean){
//also I tried:
//qr_code.visibility = View.INVISIBLE
//which gives me the same error from above
if(!serialData){
root!!.qr_code.visibility = View.VISIBLE
root!!.failed_layout.visibility = View.VISIBLE
root!!.success_layout.visibility = View.INVISIBLE
} else if(!permissionData){
root!!.qr_code.visibility = View.INVISIBLE
root!!.failed_layout.visibility = View.VISIBLE
root!!.success_layout.visibility = View.INVISIBLE
} else if(permissionData && serialData){
root!!.qr_code.visibility = View.INVISIBLE
root!!.failed_layout.visibility = View.INVISIBLE
root!!.success_layout.visibility = View.VISIBLE
}
}
}
MainActivity call to HomeFragment:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
val homeFragment: HomeFragment = HomeFragment()
homeFragment.setElementVisibility(true, true)//this parameters won't be statics
}
My xml of fragment:
<?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=".ui.home.HomeFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/success_layout"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginVertical="50dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/success_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="0dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="0dp"
android:ems="8"
android:gravity="center"
android:text="@string/success_msg"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/failed_layout"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginVertical="50dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/failed_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="0dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="0dp"
android:ems="8"
android:gravity="center"
android:text="@string/failed_msg"
android:textAlignment="center"
android:textColor="@color/design_default_color_error"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/qr_code"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginVertical="50dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/openScanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/take_scan_qr_code" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 1
Views: 637
Reputation: 6275
You can't just init val homeFragment: HomeFragment = HomeFragment()
and access it's view from Activity
. You need to attach Fragment
to activity. This is official example of usage and integration of fragments
Upvotes: 1
Reputation: 47
try this a quick solution, more details at last
class HomeFragment : Fragment(R.layout.fragment_home) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?){
super.onViewCreated(view, savedInstanceState)
setElementVisibility()
}
fun setElementVisibility(){
qr_code.visibility = View.INVISIBLE
failed_layout.visibility = View.VISIBLE
success_layout.visibility = View.INVISIBLE
}
}
So u r getting unresolve reference because as answered by bruno findviewbyId() is applied to View object. so u can use his method. In my method u will call the function only after the view is created i.e. onViewCreated(). so u can now call directly. I called the view in fragment constructor itself. This is short and easy. Thanks for more go through documentation.
Upvotes: 2
Reputation: 4007
findViewById()
is a method of the View
object.
Try this :
qrcode = root.findViewById(R.id.qr_code) as LinearLayout
Upvotes: 2