[英]Unresolved Reference for 'findViewById' for Button Click Listener through fragments
I'm trying to pass information from text fields to a function querying a database which should pass once the register button is clicked.我正在尝试将信息从文本字段传递到 function 查询数据库,一旦单击注册按钮,该数据库就应该通过。 I believe the fact I'm using fragments is affecting the listeners for this in some way though...
我相信我使用片段的事实在某种程度上影响了听众......
The code I'm attempting to run within the fragment:我试图在片段中运行的代码:
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
}
}
To call on the addUser() Function:调用 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
}
}
register.xml for the fragment为片段注册 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>
Main Activity for the Fragment manager:片段管理器的主要活动:
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()
}
}
You have to inflate the view of the fragment.您必须膨胀片段的视图。 and then use the view to set xml contents.
然后使用视图设置 xml 内容。
Sample Code:示例代码:
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;
}
It is important to return the view at the end of onCreateView()
method在
onCreateView()
方法结束时返回视图很重要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.