我正在尝试操作片段xml中的元素,但是我所做的所有尝试都导致了一些错误。当我使用root !!。qr_code.visibility = View.INVISIBLE时,我得到了:
我的元素引用有什么问题?

我的HomeFragment :(从MainActivity中的onCreate调用)

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
        }
    }
}
对HomeFragment的MainActivity调用:
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

    }
我的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=".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>

最佳答案

findViewById()View对象的方法。
试试这个 :

qrcode = root.findViewById(R.id.qr_code) as LinearLayout

10-08 09:12