嗨,我是 Android 开发的新手,我在通过 Firbase RTDB 引入 CRUD 时遇到了一些问题t 读取输入,但我添加了一个新按钮来清除视图中输入的文本,并添加了一些 println 将输入的文本打印到控制台,这些都按预期工作
class CreatePlayerFragment: Fragment(), AnkoLogger {
private lateinit var viewModel: CharacterViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewModel = ViewModelProvider(this).get(CharacterViewModel::class.java)
return inflater.inflate(R.layout.fragment_create_player, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// display message on succesful character creation, display dynamic error message on failure
viewModel.result.observe(viewLifecycleOwner, Observer {
val message = if (it == null) {
getString(R.string.character_added)
} else {0
getString(R.string.character_failure, it.message)
}
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
})
//listener for submission button
submit.setOnClickListener {
val name: String = editTextPlayerName.text.toString().trim()
val race: String = editTextRace.text.toString().trim()
val classType: String = editTextClass.text.toString().trim()
info("button clicked")
if (TextUtils.isEmpty(name)) {
input_layout_name.error = getString(R.string.field_required_error)
return@setOnClickListener
}
val character = CharacterModel()
character.name = name
character.race = race
character.classType = classType
viewModel.addCharacter(character)
println(name)
println(race)
println(classType)
}
clear.setOnClickListener {
editTextPlayerName.getText().clear()
editTextRace.getText().clear()
editTextClass.getText().clear()
}
}
}
这是我的 CharacterViewModel 的代码
class CharacterViewModel : ViewModel() {
private val dbCharacterModels = FirebaseDatabase.getInstance().getReference(NODE_CHARACTERS)
private val _result = MutableLiveData<Exception?>()
val result: LiveData<Exception?>
get() = _result
/*
Add character to DB, save a character under the characters node in the DB with a
unique key id.
*/
fun addCharacter(character: CharacterModel) {
character.id = dbCharacterModels.push().key
dbCharacterModels.child(character.id!!).setValue(character).addOnCompleteListener {
if(it.isSuccessful){
_result.value = null
} else {
_result.value = it.exception
}
}
}
}
编辑 添加了一些我提交时的日志
2020-12-27 15:56:11.824 2956-2956/? D/BoundBrokerSvc: Loading bound service for intent: Intent { act=com.google.firebase.dynamiclinks.service.START pkg=com.google.android.gms }
2020-12-27 15:56:11.843 2123-3463/? I/system_server: oneway function results will be dropped but finished with status OK and parcel size 4
2020-12-27 15:56:11.857 2956-2956/? D/BoundBrokerSvc: onBind: Intent { act=com.google.android.gms.common.BIND_SHARED_PREFS pkg=com.google.android.gms }
2020-12-27 15:56:11.857 2956-2956/? D/BoundBrokerSvc: Loading bound service for intent: Intent { act=com.google.android.gms.common.BIND_SHARED_PREFS pkg=com.google.android.gms }
2020-12-27 15:56:11.862 2956-2956/? D/BoundBrokerSvc: onUnbind: Intent { act=com.google.android.gms.common.BIND_SHARED_PREFS pkg=com.google.android.gms }
2020-12-27 15:56:11.870 2956-2956/? D/BoundBrokerSvc: onBind: Intent { act=com.google.android.gms.common.BIND_SHARED_PREFS pkg=com.google.android.gms }
2020-12-27 15:56:11.870 2956-2956/? D/BoundBrokerSvc: Loading bound service for intent: Intent { act=com.google.android.gms.common.BIND_SHARED_PREFS pkg=com.google.android.gms }
2020-12-27 15:56:11.875 2956-2956/? D/BoundBrokerSvc: onUnbind: Intent { act=com.google.android.gms.common.BIND_SHARED_PREFS pkg=com.google.android.gms }
2020-12-27 15:56:12.686 31048-31048/org.wit.guildmanagerapp I/CreatePlayerFragment: button clicked
2020-12-27 15:56:12.687 31048-31048/org.wit.guildmanagerapp I/System.out: bob
2020-12-27 15:56:12.687 31048-31048/org.wit.guildmanagerapp I/System.out: Human
2020-12-27 15:56:12.687 31048-31048/org.wit.guildmanagerapp I/System.out: Mage
2020-12-27 15:56:12.694 2123-3463/? I/system_server: oneway function results will be dropped but finished with status OK and parcel size 4
2020-12-27 15:56:12.794 173-173/? E/SELinux: avc: denied { find } for pid=4064 uid=2000 name=car_service scontext=u:r:shell:s0 tcontext=u:object_r:default_android_service:s0 tclass=service_manager permissive=0
2020-12-27 15:56:12.794 4064-4064/? W/cmd: Can't find service car_service
我读到的这个错误也可以通过在 androidManifest 中添加允许明文来修复,但它仍然出现
2020-12-27 15:56:23.831 489-489/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-12-27 15:56:23.831 489-489/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe