Home > Software engineering >  apk (android studio) failed install
apk (android studio) failed install

Time:11-10

I want to run an application on my android device, when I run it through android studio suddenly the application cannot be opened (even though the application can be installed). Can anyone help me solve this problem?

================================ MainActivity.kt

package com.rifqi_19104031.modul5

import android.*

class MainActivity : AppCompatActivity() {

    // deklarasi atribut (id xml)
    private lateinit var inputProdi : EditText
    private lateinit var btnProdi : Button
    private lateinit var btnCallBrowser : Button
    private lateinit var btnCallCamera : Button
    private lateinit var inputPhoneNumber : EditText
    private lateinit var btnCallPhone : Button
    private lateinit var btnFragment : Button

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

        // menambahkan fungsi untuk button prodi
        btnProdi.setOnClickListener {
            val namaProdi = inputProdi.text.toString()
            if (namaProdi.isEmpty()) {
                inputProdi.error = "Prodi Tidak Boleh Kosong"
                return@setOnClickListener
            }
            val moveWithDataIntent = Intent(this@MainActivity, MainActivityReadData::class.java)
            moveWithDataIntent.putExtra(MainActivityReadData.EXTRA_PRODI, namaProdi)
            startActivity(moveWithDataIntent)
        }

        // membuka web ittp
        btnCallBrowser.setOnClickListener{
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse("http://ittelkom-pwt.ac.id/")
            startActivity(intent)
        }

        // menjalankan kamera perangkat
        btnCallCamera.setOnClickListener{
            val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            startActivity(intent)
        }

        // menjalankan call phone perangkat
        btnCallPhone.setOnClickListener{
            val phoneNumber = inputPhoneNumber.getText()
            if (phoneNumber.isEmpty()) {
                inputPhoneNumber.error = "Nomor Telpon Tidak Boleh Kosong"
                return@setOnClickListener
            }
            val intent = Intent(Intent.ACTION_CALL)
            intent.data = Uri.parse("tel:"   phoneNumber)
            startActivity(intent)
        }

        // memanggil fungsi user permission
        setupPermissions()

        // menambahkan fungsi untuk tombol fragment
        btnFragment.setOnClickListener{
            val intent = Intent(this, Pratice5ForFragmentActivity::class.java)
            startActivity(intent)
        }
    }


    // menampilkan uer permission
    val CALL_REQUEST_CODE = 100
    @SuppressLint("MissingPermission")
    private fun setupPermissions() {
        val permission = ContextCompat.checkSelfPermission(this,
            Manifest.permission.CALL_PHONE)
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                arrayOf(Manifest.permission.CALL_PHONE),
                CALL_REQUEST_CODE)
        }
    }
}

i have an error

kotlin.UninitializedPropertyAccessException: lateinit property btnProdi has not been initialized

But in my MainActivity.kt class I have initialized the btnProdi variable, is my variable declaration wrong?

CodePudding user response:

As in the error, the btnProdi was not initialized. To resolve the issue, first, make sure that the components in Your layout file for the MainActivity have their own id.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    ...
    tools:context=".features.main.MainActivity">

    <Button
        android:id="@ id/btnProdi"
        ... />

</androidx.constraintlayout.widget.ConstraintLayout>

Having an id assigned to the Button in the xml file will enable us to reference the it in the kotlin code and, finally, initialize the btnProdi.

MainActivity.kt

class MainActivity : AppCompatActivity() {

   ...

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

        // NOTICE THIS LINE
        btnProdi == findViewById(R.id.btnProdi)

        btnProdi.setOnClickListener {
            ...
        }
    }
}

In the above code You will use findViewById which will return the View object identified by the id specified before. In Your case it will be a Button object. From now on the btnProdi will be initialized and ready to use.

Important

In Your code there are even more buttons that lack initialization. I'm talking about btnCallCamera, btnCallBrowser etc. You should apply the same workflow as with the btnProdi to make sure that those buttons will work too.

  • Related