我试图显示从 SQL 查询获取的两个值,并在 TextView 中显示这些值。
barcode_field_value3.text = BarcodeProcessor.data.value1
barcode_field_value2?.text = BarcodeProcessor.data.value2
我知道 value1 和 value2 具有正确的值,因为我将它们打印到控制台并且每次都是正确的。
在我的 LiveBarcodeScanningActivity 中,我尝试将上述语句放入 onCreate() 中,但当应用程序启动 Activity 时它会崩溃。
我在下面的另一部分中尝试更改文本值:
workflowModel?.detectedBarcode?.observe(this, Observer { barcode ->
if (barcode != null) {
val barcodeFieldList = ArrayList<BarcodeField>()
barcodeFieldList.add(BarcodeField("Module Serial #", barcode.rawValue ?: ""))
BarcodeResultFragment.show(supportFragmentManager, barcodeFieldList)
//This prints the values to the console as a test
println("This is the RESULTS ---- " + BarcodeProcessor.data.value1)
println("This is the RESULTS ---- " + BarcodeProcessor.data.value2)
}
barcode_field_value3.text = BarcodeProcessor.data.value1
barcode_field_value2?.text = BarcodeProcessor.data.value2
})
我尝试让这些值可以为空,但当它们为空时,什么都不会改变。
BarcodeResultFragment
/** Displays the bottom sheet to present barcode fields contained in the detected barcode. */
/**
* Controls the bottom slide in (barcode_field.xml)
*/
class BarcodeResultFragment : BottomSheetDialogFragment() {
override fun onCreateView(
layoutInflater: LayoutInflater,
viewGroup: ViewGroup?,
bundle: Bundle?
): View {
val view = layoutInflater.inflate(R.layout.barcode_bottom_sheet, viewGroup)
val arguments = arguments
val barcodeFieldList: ArrayList<BarcodeField> =
if (arguments?.containsKey(ARG_BARCODE_FIELD_LIST) == true) {
arguments.getParcelableArrayList(ARG_BARCODE_FIELD_LIST) ?: ArrayList()
} else {
Log.e(TAG, "No barcode field list passed in!")
ArrayList()
}
view.findViewById<RecyclerView>(R.id.barcode_field_recycler_view).apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(activity)
adapter = BarcodeFieldAdapter(barcodeFieldList)
}
return view
}
override fun onDismiss(dialogInterface: DialogInterface) {
activity?.let {
// Back to working state after the bottom sheet is dismissed.
ViewModelProviders.of(it).get<WorkflowModel>(WorkflowModel::class.java)
.setWorkflowState(WorkflowState.DETECTING)
}
super.onDismiss(dialogInterface)
}
companion object {
private const val TAG = "BarcodeResultFragment"
private const val ARG_BARCODE_FIELD_LIST = "arg_barcode_field_list"
fun show(fragmentManager: FragmentManager, barcodeFieldArrayList: ArrayList<BarcodeField>) {
val barcodeResultFragment = BarcodeResultFragment()
barcodeResultFragment.arguments = Bundle().apply {
putParcelableArrayList(ARG_BARCODE_FIELD_LIST, barcodeFieldArrayList)
}
barcodeResultFragment.show(fragmentManager, TAG)
}
fun dismiss(fragmentManager: FragmentManager) {
(fragmentManager.findFragmentByTag(TAG) as BarcodeResultFragment?)?.dismiss()
}
}
}
最佳答案
请尝试这个
val requestCode=100
startActivityForResult(Intent(context,MainActivity::class.java),requestCode)
和条码扫描仪 Activity 中
val data = Intent();
val text = "Result to be returned...."
//---set the data to pass back---
data.setData("Your Data");
setResult(RESULT_OK, data);
//---close the activity---
finish()
并像这样进入主要 Activity
override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent):Void {
if (requestCode == requestCode) {
if (resultCode == RESULT_OK) {
}
}
}
关于java.lang.IllegalStateException : [TextView] must not be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57076501/