【问题标题】:Unresolved Reference for 'findViewById' for Button Click Listener through fragments通过片段的按钮单击侦听器的 \'findViewById\' 的未解决引用
【发布时间】:2022-11-18 14:14:54
【问题描述】:

我正在尝试将信息从文本字段传递给查询数据库的函数,一旦登记按钮被点击。我相信我使用片段的事实在某种程度上影响了听众......

Screenshot of Error

我试图在片段中运行的代码:

package ie.ul.frankscafe.View

import androidx.fragment.app.Fragment
import ie.ul.frankscafe.R

public class Register : Fragment(R.layout.register)  {
    // get reference to button
    val registerButton = findViewById(R.id.registerButton) as androidx.appcompat.widget.AppCompatButton
// set on-click listener
    registerButton.setOnClickListener {
        //pass information from text fields to addUser
    }
}

要调用 addUser() 函数:

package ie.ul.frankscafe.ViewModel

import android.app.Application
import androidx.lifecycle.*
import ie.ul.frankscafe.Model.db.AppDatabase
import ie.ul.frankscafe.Model.db_entity.User
import ie.ul.frankscafe.Model.entity.UserEntity
import ie.ul.frankscafe.repository.UserRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch


class UserViewModel(application: Application): AndroidViewModel(application) {
     val getAll : List<User>
     val repository: UserRepository

    init {
        val userDao = AppDatabase.getDatabase(application).UserDao()
        repository = UserRepository(userDao)
        getAll = repository.getAll
    }


    fun addUser(user: User){
        viewModelScope.launch(Dispatchers.IO){
            repository.addUser(user)
        }
    }

    fun findbyusername(username: String): User {
        var temp = repository.findbyusername(username)
        return temp
    }



}

片段的 register.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/quaternary">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Welcome to Frank's Cafe !"
        android:textColor="@color/secondary"
        android:textSize="30dp"
        android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="50dp"
            android:text="Welcome to Frank's Cafe !"
            android:textColor="@color/secondary"
            android:textSize="30dp"
            android:textStyle="bold" />


    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/registerUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="250dp"
        android:layout_marginRight="20dp"
        android:hint="username" />

    <EditText
            android:id="@+id/registerPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="320dp"
            android:layout_marginRight="20dp"
            android:ems="10"
            android:inputType="textPassword"
            android:hint="password"/>


    <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/registerButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/registerPassword"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="100dp"
            android:layout_marginRight="20dp"
            android:background="@color/secondary"
            android:text="register"
            android:textColor="@color/quinary" />

</RelativeLayout>

片段管理器的主要活动:

package ie.ul.frankscafe.View

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.fragment.app.Fragment
import ie.ul.frankscafe.R
import ie.ul.frankscafe.ViewModel.FoodViewModel
import ie.ul.frankscafe.databinding.ActivityMainBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)


        binding.fragment1btn.setOnClickListener {

            replaceFragment(Signin())

        }

        binding.fragment2btn.setOnClickListener {

            replaceFragment(Register())

        }

 }
    private fun replaceFragment(fragment : Fragment){

        val fragmentManager = supportFragmentManager
        val fragmentTransaction = fragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragment_container, fragment)
        fragmentTransaction.commit()
    }
}


【问题讨论】:

    标签: android kotlin android-fragments onclicklistener


    【解决方案1】:

    您必须膨胀片段的视图。然后使用视图设置 xml 内容。

    示例代码:

    public class IssueFragment extends Fragment {
    
        private View v;
    
        private TextView atm;
    
        public IssueFragment(){
            // require a empty public constructor
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            v = View.inflate(getContext(),R.layout.fragment_issue, null);
    
            atm = v.findViewById(R.id.atm_tv);
    
    return v;
    }
    

    onCreateView()方法结束时返回视图很重要

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2013-09-13
      • 1970-01-01
      • 2017-11-09
      • 2016-12-19
      相关资源
      最近更新 更多