繁体   English   中英

通过片段的按钮单击侦听器的“findViewById”的未解决参考

[英]Unresolved Reference for 'findViewById' for Button Click Listener through fragments

我正在尝试将信息从文本字段传递到 function 查询数据库,一旦单击注册按钮,该数据库就应该通过。 我相信我使用片段的事实在某种程度上影响了听众......

错误截图

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

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() Function:

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
    }



}

为片段注册 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()
    }
}


您必须膨胀片段的视图。 然后使用视图设置 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()方法结束时返回视图很重要

①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM