java.lang.IllegalStateException : [TextView] must not be null

标签 java android android-layout kotlin

我试图显示从 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/

相关文章:

android - 在 android : "The method getWindowManager is undefined" 上获取窗口大小时出错

java - 如何动态有效地应用 RTL

java - 不幸的是,App已停止(Android开发)

java - 如何在 java 中声明一个 32 位整数?

Android Studio 在截屏时卡住

android - 以编程方式检测 onePlus One 设备中软导航栏的可用性?

Android 数据绑定(bind) - 找不到属性的 getter

java - 如果一个应该在退出时加密信息的程序崩溃了

java - 如何在不禁用进一步日志记录的情况下清除 catalina.out?

android - 打开应用程序后没有任何显示