Home > Software engineering >  Cannot solve this error? Kotlin (QR Reader)
Cannot solve this error? Kotlin (QR Reader)

Time:10-07

I code this to build QR reader.

class scannerActivity : AppCompatActivity() {

    private var qrEader: QREader ?= null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scanner)


        val Button = findViewById<Button>(R.id.button6)
        Button.setOnClickListener {
            startActivity(Intent(this@scannerActivity, conductorMenu::class.java))}

        Dexter.withActivity(this@scannerActivity)
            .withPermissions(Manifest.permission.CAMERA)
            .withListener(object:PermissionListener, MultiplePermissionsListener {
                override fun onPermissionGranted(response: PermissionGrantedResponse?) {
                    setupCamera()
                }

                override fun onPermissionDenied(response: PermissionDeniedResponse?) {
                    Toast.makeText(this@scannerActivity, "You must enable this permission", Toast.LENGTH_LONG).show()
                }

                override fun onPermissionRationaleShouldBeShown(
                    permission: PermissionRequest?,
                    token: PermissionToken?
                ) {

                }

                override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
                    TODO("Not yet implemented")
                }

                override fun onPermissionRationaleShouldBeShown(
                    permissions: MutableList<PermissionRequest>?,
                    token: PermissionToken?
                ) {

                }


            }).check()

    }

    private fun setupCamera() {
        val ToggleButton = findViewById<ToggleButton>(R.id.toggleButton)
        ToggleButton.setOnClickListener{
            if (qrEader!!.isCameraRunning){
                ToggleButton.text = "START"
                qrEader!!.stop()
            }
            else{
                ToggleButton.text = "STOP"
                qrEader!!.start()
            }
        }

        setupQrEader()
    }

    @SuppressLint("WrongViewCast")
    private fun setupQrEader() {
        val camera_view = findViewById<SurfaceView>(R.id.camera_view)
        val code_info = findViewById<TextView>(R.id.textView9)

        qrEader = QREader.Builder(this,camera_view, QRDataListener { data ->
            code_info.post { code_info.text = data }
        }).facing(QREader.BACK_CAM)
            .enableAutofocus(true)
            .height(camera_view.height)
            .width(camera_view.width)
            .build()
    }

    override fun onResume() {
        super.onResume()
        val camera_view = findViewById<SurfaceView>(R.id.camera_view)

        if (qrEader != null)
            qrEader!!.initAndStart(camera_view)
    }

    override fun onPause() {
        super.onPause()
        if (qrEader != null)
            qrEader!!.releaseAndCleanup()
    }
}

This's my XML File

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".scannerActivity">

    <FrameLayout
        android:id="@ id/scannerInfo"
        android:layout_width="408dp"
        android:layout_height="567dp">

        <SurfaceView
            android:id="@ id/camera_view"
            android:layout_width="match_parent"
            android:layout_height="567dp" />

        <View
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:background="@drawable/box_shape" />

    </FrameLayout>

    <TextView
        android:id="@ id/textView9"
        android:layout_width="153dp"
        android:layout_height="28dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.12"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.916"
        android:layout_marginTop="580dp"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@ id/button6"
        android:layout_width="74dp"
        android:layout_height="38dp"
        android:text="Back"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.949"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.973"
        android:layout_marginTop="600dp"
        android:layout_marginLeft="310dp"/>

    <ToggleButton
        android:id="@ id/toggleButton"
        android:layout_width="101dp"
        android:layout_height="76dp"
        android:text="ToggleButton"
        tools:layout_editor_absoluteX="205dp"
        tools:layout_editor_absoluteY="578dp"
        tools:ignore="MissingConstraints"
        android:backgroundTint="@color/purple_500"
        android:layout_marginTop="590dp"
        android:layout_marginLeft="180dp"/>

    <EditText
        android:id="@ id/editTextTextPersonName"
        android:layout_width="154dp"
        android:layout_height="38dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.126"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="610dp"
        app:layout_constraintVertical_bias="0.991" />

</RelativeLayout>

This's the error I got. I can't understand that. It says constraintlayout but it has RelativeLayout. Please help me, I'm new to kotlin.

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.busconductor/com.example.busconductor.ConductorActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class androidx.constraintlayout.widget.ConstraintLayout

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class androidx.constraintlayout.widget.ConstraintLayout

This's the line what highlighted. this.setContentView(R.layout.activity_conductor)

Edit

activity_conductor_menu.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back"
    tools:context=".conductorMenu">

    <Button
        android:id="@ id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Scanner"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@ id/button4"
        app:layout_constraintHorizontal_bias="0.396"
        app:layout_constraintStart_toEndOf="@ id/button3"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.924"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@ id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:text="City Codes"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.923"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@ id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="fantasy"
        android:text="A  Conductor"
        android:textColor="#FF000000"
        android:textSize="40sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.519"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.787" />

    <Button
        android:id="@ id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginRight="4dp"
        android:text="LogOut"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.923"
        tools:ignore="MissingConstraints" />

    <ImageView
        android:id="@ id/imageView"
        android:layout_width="340dp"
        android:layout_height="445dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.619"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.149"
        app:srcCompat="@drawable/final_logo_modified"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

Try to remove this line android:background="@drawable/back" from the layout activity_conductor and see if it will work well..

  • Related