如何跨越多个活动
我需要帮助从一项活动转移到另一项活动。我是一个初学者,只是学习如何使用Kotlin来构建应用程序。
因此,我的项目包含大约14个活动,当我分别单击“继续”或“ Back”按钮时,我需要能够从一个活动转移到另一个活动和向后移动。
我已经能够使用“意图”从主要活动导航到第二个活动,
但是,当我尝试从第二个活动转到第三个活动时,该应用程序不断将我返回MainAttivity .....和如果我再次这样做,那就崩溃了。
这是我的主攻击代码;
package com.example.mywaterlow02
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val buttonStart: Button = findViewById(R.id.button_start)
buttonStart.setOnClickListener {
val intentMain = Intent (this, GenderPg::class.java)
startActivity(intentMain)
}
}
}
...这是第二个活动代码
package com.example.mywaterlow02
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class GenderPg : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_gender_pg)
val genderBack : Button = findViewById(R.id.btn_gender_back)
genderBack.setOnClickListener {
val intentGender = Intent (this, MainActivity::class.java)
startActivity(intentGender)
}
val genderContinue : Button = findViewById(R.id.btn_gender_continue)
genderContinue.setOnClickListener {
val intentGender2 = Intent (this, AgePg02::class.java)
startActivity(intentGender2)
}
}
}
,这是第三个活动代码
package com.example.mywaterlow02
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class AgePg02 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_age_pg02)
val ageBack : Button = findViewById(R.id.age_back)
ageBack.setOnClickListener {
val intentAge01 = Intent (this, GenderPg::class.java)
startActivity(intentAge01)
}
val ageContinue : Button = findViewById(R.id.age_continue)
ageContinue.setOnClickListener {
val intentAge02 = Intent (this, BmiPg02::class.java)
startActivity(intentAge02)
}
}
}
androidManifest XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mywaterlow02">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar">
<activity
android:name=".BmiPg02"
android:exported="false" />
<activity
android:name=".AgePg02"
android:exported="false" />
<activity
android:name=".GenderPg"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
MainActivity XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@color/Primary"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:fontFamily="@font/courier_prime_bold"
android:text="WATERLOW"
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/courier_prime_bold"
android:text="Calculator"
android:textSize="45sp"/>
<Button
android:id="@+id/button_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40sp"
android:layout_marginTop="50dp"
android:layout_marginRight="40sp"
android:height="70dp"
android:backgroundTint="@color/Secondary"
android:fontFamily="@font/courier_prime_bold"
android:text="Start"
android:textColor="@color/white"
android:textSize="25sp" />
</LinearLayout>
第二活动XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="@color/Primary"
android:fillViewport="true"
tools:context=".GenderPg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/courier_prime_bold"
android:text="Gender"
android:layout_marginTop="100sp"
android:layout_marginStart="18sp"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/gender_female"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="35dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="10dp"
android:fontFamily="@font/courier_prime_bold"
android:background="@drawable/shadowy_bckgrd"
android:padding="20dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/female"
tools:text="Female"
/>
<TextView
android:id="@+id/gender_male"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="10dp"
android:background="@drawable/shadowy_bckgrd"
android:fontFamily="@font/courier_prime_bold"
android:padding="15dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/male"
tools:text="Male" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginTop="-10dp"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_gender_back"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_marginStart="22dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="10dp"
android:height="70dp"
android:fontFamily="@font/courier_prime_bold"
android:text="Back"
android:textColor="@color/black"
android:textSize="20dp"
app:cornerRadius="5dp"
app:strokeColor="@color/black"
app:strokeWidth="1dp"
android:textAllCaps="false"/>
<Button
android:id="@+id/btn_gender_continue"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_marginHorizontal="31dp"
android:layout_marginStart="38dp"
android:layout_marginTop="30dp"
android:height="70dp"
android:backgroundTint="@color/Secondary"
android:fontFamily="@font/courier_prime_bold"
android:text="Continue"
android:textColor="@color/white"
android:textSize="20sp"
android:textAllCaps="false"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
第三活动XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="@color/Primary"
android:fillViewport="true"
tools:context=".AgePg02">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/courier_prime_bold"
android:text="Age"
android:layout_marginTop="100sp"
android:layout_marginStart="35sp"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@id/age_back"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginStart="35dp"
android:layout_marginTop="35dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:fontFamily="@font/courier_prime_bold"
android:background="@drawable/shadowy_bckgrd"
android:padding="20dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/_14_49"
tools:text="14 - 49"
/>
<TextView
android:id="@+id/age_5064"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginStart="35dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/shadowy_bckgrd"
android:fontFamily="@font/courier_prime_bold"
android:padding="15dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/_50_to_64"
tools:text="50 - 64" />
<TextView
android:id="@+id/age_6574"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginStart="35dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/shadowy_bckgrd"
android:fontFamily="@font/courier_prime_bold"
android:padding="15dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
tools:text="65 - 74" />
<TextView
android:id="@+id/age_7580"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginStart="35dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/shadowy_bckgrd"
android:fontFamily="@font/courier_prime_bold"
android:padding="15dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/_75_to_80"
tools:text="75 - 80" />
<TextView
android:id="@+id/age_81plus"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginStart="35dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/shadowy_bckgrd"
android:fontFamily="@font/courier_prime_bold"
android:padding="15dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/over_81"
tools:text="81+" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginTop="-10dp"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/age_back"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="140dp"
android:layout_height="75dp"
android:layout_marginStart="40dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="10dp"
android:height="70dp"
android:fontFamily="@font/courier_prime_bold"
android:text="Back"
android:textColor="@color/black"
android:textSize="18dp"
android:textAllCaps="false"
app:cornerRadius="5dp"
app:strokeColor="@color/black"
app:strokeWidth="1dp" />
<Button
android:id="@+id/age_continue"
android:layout_width="140dp"
android:layout_height="75dp"
android:layout_marginTop="50dp"
android:height="70dp"
android:backgroundTint="@color/Secondary"
android:fontFamily="@font/courier_prime_bold"
android:text="Continue"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
我在做什么错?.....为什么我不能从第二个活动转到第三个活动活动?
这是我在logcat中看到的错误消息……我不知道如何修复它或它的含义..
2022-05-06 08:53:02.992 12622-12622/com.example.mywaterlow02 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mywaterlow02, PID: 12622
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mywaterlow02/com.example.mywaterlow02.AgePg02}: java.lang.ClassCastException: com.google.android.material.textview.MaterialTextView cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.ClassCastException: com.google.android.material.textview.MaterialTextView cannot be cast to android.widget.Button
at com.example.mywaterlow02.AgePg02.onCreate(AgePg02.kt:13)
at android.app.Activity.performCreate(Activity.java:8051)
at android.app.Activity.performCreate(Activity.java:8031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
请我该怎么办?
I need help with moving from one activity to another. I am a beginner just learning how to use Kotlin to build Apps.
So my project contains about 14 activities and I need to be able to move from one activity to the other and backwards when I click on the "continue" or "back" buttons respectively.
I have been able to use "Intent" to navigate from the Mainactivity to the second activity,
however, when I tried to go from the second activity to the third activity, the app keeps returning me back to the MainActivity.....and if I do it again, it crashes.
This is my MainActivity code;
package com.example.mywaterlow02
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val buttonStart: Button = findViewById(R.id.button_start)
buttonStart.setOnClickListener {
val intentMain = Intent (this, GenderPg::class.java)
startActivity(intentMain)
}
}
}
...and this is the second activity code
package com.example.mywaterlow02
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class GenderPg : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_gender_pg)
val genderBack : Button = findViewById(R.id.btn_gender_back)
genderBack.setOnClickListener {
val intentGender = Intent (this, MainActivity::class.java)
startActivity(intentGender)
}
val genderContinue : Button = findViewById(R.id.btn_gender_continue)
genderContinue.setOnClickListener {
val intentGender2 = Intent (this, AgePg02::class.java)
startActivity(intentGender2)
}
}
}
and this is the third activity code
package com.example.mywaterlow02
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class AgePg02 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_age_pg02)
val ageBack : Button = findViewById(R.id.age_back)
ageBack.setOnClickListener {
val intentAge01 = Intent (this, GenderPg::class.java)
startActivity(intentAge01)
}
val ageContinue : Button = findViewById(R.id.age_continue)
ageContinue.setOnClickListener {
val intentAge02 = Intent (this, BmiPg02::class.java)
startActivity(intentAge02)
}
}
}
AndroidManifest XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mywaterlow02">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar">
<activity
android:name=".BmiPg02"
android:exported="false" />
<activity
android:name=".AgePg02"
android:exported="false" />
<activity
android:name=".GenderPg"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
MainActivity XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@color/Primary"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:fontFamily="@font/courier_prime_bold"
android:text="WATERLOW"
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/courier_prime_bold"
android:text="Calculator"
android:textSize="45sp"/>
<Button
android:id="@+id/button_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40sp"
android:layout_marginTop="50dp"
android:layout_marginRight="40sp"
android:height="70dp"
android:backgroundTint="@color/Secondary"
android:fontFamily="@font/courier_prime_bold"
android:text="Start"
android:textColor="@color/white"
android:textSize="25sp" />
</LinearLayout>
Second Activity XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="@color/Primary"
android:fillViewport="true"
tools:context=".GenderPg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/courier_prime_bold"
android:text="Gender"
android:layout_marginTop="100sp"
android:layout_marginStart="18sp"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/gender_female"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="35dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="10dp"
android:fontFamily="@font/courier_prime_bold"
android:background="@drawable/shadowy_bckgrd"
android:padding="20dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/female"
tools:text="Female"
/>
<TextView
android:id="@+id/gender_male"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="10dp"
android:background="@drawable/shadowy_bckgrd"
android:fontFamily="@font/courier_prime_bold"
android:padding="15dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/male"
tools:text="Male" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginTop="-10dp"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_gender_back"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_marginStart="22dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="10dp"
android:height="70dp"
android:fontFamily="@font/courier_prime_bold"
android:text="Back"
android:textColor="@color/black"
android:textSize="20dp"
app:cornerRadius="5dp"
app:strokeColor="@color/black"
app:strokeWidth="1dp"
android:textAllCaps="false"/>
<Button
android:id="@+id/btn_gender_continue"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_marginHorizontal="31dp"
android:layout_marginStart="38dp"
android:layout_marginTop="30dp"
android:height="70dp"
android:backgroundTint="@color/Secondary"
android:fontFamily="@font/courier_prime_bold"
android:text="Continue"
android:textColor="@color/white"
android:textSize="20sp"
android:textAllCaps="false"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Third Activity XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="@color/Primary"
android:fillViewport="true"
tools:context=".AgePg02">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/courier_prime_bold"
android:text="Age"
android:layout_marginTop="100sp"
android:layout_marginStart="35sp"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@id/age_back"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginStart="35dp"
android:layout_marginTop="35dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:fontFamily="@font/courier_prime_bold"
android:background="@drawable/shadowy_bckgrd"
android:padding="20dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/_14_49"
tools:text="14 - 49"
/>
<TextView
android:id="@+id/age_5064"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginStart="35dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/shadowy_bckgrd"
android:fontFamily="@font/courier_prime_bold"
android:padding="15dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/_50_to_64"
tools:text="50 - 64" />
<TextView
android:id="@+id/age_6574"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginStart="35dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/shadowy_bckgrd"
android:fontFamily="@font/courier_prime_bold"
android:padding="15dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
tools:text="65 - 74" />
<TextView
android:id="@+id/age_7580"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginStart="35dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/shadowy_bckgrd"
android:fontFamily="@font/courier_prime_bold"
android:padding="15dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/_75_to_80"
tools:text="75 - 80" />
<TextView
android:id="@+id/age_81plus"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginStart="35dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/shadowy_bckgrd"
android:fontFamily="@font/courier_prime_bold"
android:padding="15dp"
android:paddingStart="25dp"
android:textColor="#7A8089"
android:textSize="18sp"
android:text="@string/over_81"
tools:text="81+" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginTop="-10dp"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/age_back"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="140dp"
android:layout_height="75dp"
android:layout_marginStart="40dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="10dp"
android:height="70dp"
android:fontFamily="@font/courier_prime_bold"
android:text="Back"
android:textColor="@color/black"
android:textSize="18dp"
android:textAllCaps="false"
app:cornerRadius="5dp"
app:strokeColor="@color/black"
app:strokeWidth="1dp" />
<Button
android:id="@+id/age_continue"
android:layout_width="140dp"
android:layout_height="75dp"
android:layout_marginTop="50dp"
android:height="70dp"
android:backgroundTint="@color/Secondary"
android:fontFamily="@font/courier_prime_bold"
android:text="Continue"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Please what am I doing wrong?.....why can't I go from second activity to third activity?
This is the error message I saw in my logcat......I have no idea how to fix it or what it means..
2022-05-06 08:53:02.992 12622-12622/com.example.mywaterlow02 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mywaterlow02, PID: 12622
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mywaterlow02/com.example.mywaterlow02.AgePg02}: java.lang.ClassCastException: com.google.android.material.textview.MaterialTextView cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.ClassCastException: com.google.android.material.textview.MaterialTextView cannot be cast to android.widget.Button
at com.example.mywaterlow02.AgePg02.onCreate(AgePg02.kt:13)
at android.app.Activity.performCreate(Activity.java:8051)
at android.app.Activity.performCreate(Activity.java:8031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Please who should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Activity
agepg02
开始启动,但在
increate()
中崩溃。原因是该
活动
在您的布局XML文件中,您具有button
和atextView
,并带有此ID:Activity
AgePg02
gets started but crashes within
onCreate()
.The reason is that in your layout XML file for that
Activity
you have both aButton
and aTextView
with this ID:看来
@ID/age_back
是一个文本视图,您正在尝试将其施加到第三个活动中的按钮,在线行val ageback:button = button = findViewById(r.id.age_back)
。您可以将TextView更改为第三个活动XML 中的按钮,也可以使用
Val Ageback:TextView = FindViewById(R.Id.age_back)
更正该行。It seems that
@id/age_back
is a TextView and you are trying to cast it to a Button in the third activity, on line lineval ageBack : Button = findViewById(R.id.age_back)
.You can either change the TextView to a Button in Third Activity XML or correct that line with
val ageBack : TextView = findViewById(R.id.age_back)
.