0

I'm new to android development. I'm learning how to make an app in Kotlin. On my MainActivity.kt, I have the code below:

recordBtn.setOnClickListener{
            val intent = Intent(this, com.example.testapp.data.RecordActivity::class.java)
            startActivity(intent)
        }

For some reason, I can use the update password and logout buttons on the screen, but when I press the record button to go to activity_record.xml I just get a blank white screen. This is the onCreate for my RecordActivity.kt:

@RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        setContentView(R.layout.activity_record)

        Log.e("Record Activity","Reached the onCreate of the RecordActivity")

        requestPermissions(permissions, REQUEST_RECORD_AUDIO_PERMISSION)

//        recordbtn = findViewById(R.id.recordbtn)
//        playbtn = findViewById(R.id.playbtn)
        recordbtn = RecordButton(this)
        playbtn = PlayButton(this)

        filename = "${externalCacheDir?.absolutePath}/recording.mp3"

        mr = MediaRecorder()
        mr?.setAudioSource(MediaRecorder.AudioSource.MIC)
        mr?.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
        mr?.setOutputFile(filename)
        mr?.setAudioEncoder(3) //AAC; need to review what the best choice here is.
        mr?.prepare()
    }

I feel like the problem might be that my RecordActivity.kt is in another folder than the other activities.

enter image description here

whatwhatwhat
  • 1,991
  • 4
  • 31
  • 50
  • 1
    You've overridden the wrong `onCreate()`. You want the one without the `PersistableBundle?` parameter. – Mike M. Sep 05 '20 at 03:48
  • @MikeM. that fixed the problem. Care to put it as an answer with more detail? I don't understand why that worked, but it did, and it might help others in the future. – whatwhatwhat Sep 05 '20 at 03:57
  • 1
    It's actually a pretty common issue, so I went ahead and marked it as a duplicate of a couple of previous similar posts. Anyhoo, the reason it worked is because only the `onCreate(Bundle?)` overload is called during normal `Activity` creation. The `onCreate(Bundle?, PersistableBundle?)` is used to restore state persisted across reboots. Your `onCreate()` override wasn't running at all, so `setContentView()` was never called, and you were left with an empty `Activity`. – Mike M. Sep 05 '20 at 04:01
  • 1
    Ah, makes sense! – whatwhatwhat Sep 05 '20 at 05:37

0 Answers0