我试图显示从SQL查询获得的两个值,并在TextViews中显示这些值。
barcode_field_value3.text = BarcodeProcessor.data.value1
barcode_field_value2?.text = BarcodeProcessor.data.value2
我知道value1和value2具有正确的值,因为我将它们打印到控制台,并且每次都正确。
在我的LiveBarcodeScanningActivity中,我尝试将上述语句放入onCreate()内,但在应用程序启动活动时它立即崩溃。
我在下面的另一部分中尝试更改文本值:
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
})
我试图使这些值可为空,但是当它们变为null时就没有任何改变。
/** 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()
}
}
}
答案 0 :(得分:1)
错误显示“ barcode_field_value3不能为空”。 验证代码是否仅在初始化Barcode_field_value3成员之后运行
答案 1 :(得分:1)
请尝试这个
val requestCode=100
startActivityForResult(Intent(context,MainActivity::class.java),requestCode)
并在条形码扫描器中活动
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()
并参加这样的主要活动
override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent):Void {
if (requestCode == requestCode) {
if (resultCode == RESULT_OK) {
}
}
}
答案 2 :(得分:0)
在片段中,您必须使用这样的小部件
view.barcode_field_value3.text=
view.barcode_field_value2.text=
这里视图是主要布局,并在onCreateView中这样声明它
view = inflater.inflate(R.layout.fragment_dashboard, container, false)