有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 java。lang.IllegalStateException:[TextView]不能为null

我试图显示从SQL查询中获得的两个值,并在TextView中显示这些值

   barcode_field_value3.text = BarcodeProcessor.data.value1
   barcode_field_value2?.text = BarcodeProcessor.data.value2

我知道value1&;值2具有正确的值,因为我将它们打印到控制台,并且每次都是正确的

在我的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
        })

我试着让这些值为空,但当它们为空时,什么都不会改变

条形码结果碎片

/** 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()
        }
    }
}

共 (2) 个答案

  1. # 1 楼答案

    在fragment中,您必须使用这样的小部件

    view.barcode_field_value3.text=
    view.barcode_field_value2.text=
    

    这里的视图是主布局,在onCreateView中声明如下

    view = inflater.inflate(R.layout.fragment_dashboard, container, false)
    
  2. # 2 楼答案

    请试试这个

    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) {
    
        }
    }
    }