2021年3月26日星期五

Kotlin: clicking button to go to new activity results in crash

I'm new to Kotlin and Android Studio and I tried to find a solution to this problem and cannot find it on my own.

There are two activities and I want to jump to the second activity when a button is clicked. The button's id is "startbutton". The class of the second activity is named "SecondScreen".

This is my code, I double checked it with several tutorials and still cannot find the error that results in the crash of the app, the crash occurs while running the app as soon as I click on the button:

package com.example.kotlintestgebiet    import androidx.appcompat.app.AppCompatActivity  import android.os.Bundle  import android.widget.Button  import android.content.Intent  import android.view.View  import kotlinx.android.synthetic.main.activity_title_screen.*    class TitleScreen : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {          super.onCreate(savedInstanceState)          setContentView(R.layout.activity_title_screen)          title = "KotlinApp"            //At this point I also tried           // val button = findViewById<Button>(R.id.startbutton)           // and then button.setOnClickListener           // but it didn't work either.             startbutton.setOnClickListener{              val intent = Intent(this@TitleScreen, SecondScreen::class.java)              startActivity(intent)          }      }  }      

This is the code of the Second activity:

package com.example.kotlintestgebiet    import androidx.appcompat.app.AppCompatActivity  import android.os.Bundle  import android.widget.Button  import android.content.Intent  import android.media.MediaPlayer  import android.view.View    var mMediaPlayer: MediaPlayer? = null    class SecondScreen : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {          super.onCreate(savedInstanceState)          setContentView(R.layout.activity_second_screen)        }        // 1. Plays the sound      fun playSound(view: View) {          if (mMediaPlayer == null) {              mMediaPlayer = MediaPlayer.create(this, R.raw.julie1)              mMediaPlayer!!.isLooping = true              mMediaPlayer!!.start()          } else mMediaPlayer!!.start()      }        // 2. Pause playback      fun pauseSound(view: View) {          if (mMediaPlayer != null && mMediaPlayer!!.isPlaying) mMediaPlayer!!.pause()      }        // 3. {optional} Stops playback      fun stopSound(view: View) {          if (mMediaPlayer != null) {              mMediaPlayer!!.stop()              mMediaPlayer!!.release()              mMediaPlayer = null          }      }        // 4. Closes the MediaPlayer when the app is closed      override fun onStop() {          super.onStop()          if (mMediaPlayer != null) {              mMediaPlayer!!.release()              mMediaPlayer = null          }      }  }  

If there isn't a mistake in this piece of code: what mistakes is a beginner likely to make that results in the crash? Any help would be greatly appreciated. Have a nice day!

LOGCAT error message when clicking the button in the app:

E/AndroidRuntime: FATAL EXCEPTION: main      Process: com.example.kotlintestgebiet, PID: 7666      android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.kotlintestgebiet/com.example.kotlintestgebiet.SecondScreen}; have you declared this activity in your AndroidManifest.xml?          at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2074)          at android.app.Instrumentation.execStartActivity(Instrumentation.java:1720)          at android.app.Activity.startActivityForResult(Activity.java:5258)          at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)          at android.app.Activity.startActivityForResult(Activity.java:5203)          at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)          at android.app.Activity.startActivity(Activity.java:5587)          at android.app.Activity.startActivity(Activity.java:5555)          at com.example.kotlintestgebiet.TitleScreen$onCreate$1.onClick(TitleScreen.kt:22)          at android.view.View.performClick(View.java:7862)          at android.widget.TextView.performClick(TextView.java:15004)          at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967)          at android.view.View.performClickInternal(View.java:7831)          at android.view.View.access$3600(View.java:879)          at android.view.View$PerformClick.run(View.java:29359)          at android.os.Handler.handleCallback(Handler.java:883)          at android.os.Handler.dispatchMessage(Handler.java:100)          at android.os.Looper.loop(Looper.java:237)          at android.app.ActivityThread.main(ActivityThread.java:8167)          at java.lang.reflect.Method.invoke(Native Method)          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)    
https://stackoverflow.com/questions/66750943/kotlin-clicking-button-to-go-to-new-activity-results-in-crash March 23, 2021 at 01:26AM

没有评论:

发表评论