I encountered a situation where I could not start a new activity when switching activities, and the simulator restarted the program by itself.
This is the activity before the switch
private fun login(email:String,password:String){
//Login function of customer
cAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Toast.makeText(this,"Sucessful login", Toast.LENGTH_SHORT).show()
val intent = Intent(this, custMainMenu::class.java)
startActivity(intent)
} else {
// If sign in fails, display a message to the user.
Toast.makeText(this,"Wrong password or email", Toast.LENGTH_SHORT).show()
}
}
}
This is its xml
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/login_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RegisterAndLogin.CustLoginActivity"
android:background="@color/white">
<FrameLayout
android:id="@+id/custLogin_header"
android:layout_width="wrap_content"
android:layout_height="@dimen/auth_header_image_height"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="image"
android:src="@drawable/header"
android:scaleType="fitXY"/>
</FrameLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
android:layout_marginTop="35dp"
android:textSize="@dimen/title_textSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/custLogin_header"
android:id="@+id/titleText"
android:text="@string/cust_login_title"
android:textColor="@color/black"
>
</EditText>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
android:id="@+id/layout_et_custEmailID"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/titleText"
android:hint="@string/et_hint_id"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:textColorHint="@color/black"
><EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_custID"
android:inputType="text"
android:padding="@dimen/et_padding"
android:textColor="@color/black"
android:textSize="@dimen/et_textSize"
></EditText>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
android:id="@+id/layout_et_custPassword"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/layout_et_custEmailID"
android:hint="@string/et_hint_password"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:textColorHint="@color/black"
><EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_custPassword"
android:inputType="textPassword"
android:padding="@dimen/et_padding"
android:textColor="@color/black"
android:textSize="@dimen/et_textSize"
></EditText>
</com.google.android.material.textfield.TextInputLayout>
<EditText
android:id="@+id/tv_forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:padding="@dimen/clickable_text_padding"
android:text="@string/forgot_pass"
android:textColor="@color/black"
android:textSize="@dimen/forgot_password"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/layout_et_custPassword"
tools:ignore="MissingConstraints" />
<com.example.finalyearproject.util.NDIButton
android:id="@+id/custLogin_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="67dp"
android:text="@string/btn_login_text"
app:layout_constraintTop_toBottomOf="@+id/layout_et_custPassword"
tools:layout_editor_absoluteX="158dp"
android:background="@drawable/button_background"
android:gravity="center"
tools:ignore="MissingConstraints" />
<com.example.finalyearproject.util.NDITextViewItalic
android:id="@+id/tv_custRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:foreground="?attr/selectableItemBackground"
android:padding="@dimen/clickable_text_padding"
android:text="Do not have account?Register"
android:textColor="@color/black"
android:textSize="@dimen/lbl_textSize"
app:layout_constraintTop_toBottomOf="@+id/custLogin_btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="109dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the activity after switching
class custMainMenu : AppCompatActivity() {
private lateinit var bottomMenu:BottomNavigationView
private lateinit var chatButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cust_main_menu)
bottomMenu = findViewById(R.id.bottomMenu)
chatButton=findViewById(R.id.chatButton)
val navigationController = findNavController(R.id.navigate1)
bottomMenu.setupWithNavController(navigationController)
chatButton.setOnClickListener(){
val intent= Intent(this,CustMainActivity::class.java)
startActivity(intent)
}
}
}
This is the xml for second activity
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".custMainMenu"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/productRView"
android:layout_width="match_parent"
android:layout_height="667dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/bottomMenu"
tools:listitem="@layout/product_item_layout" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:menu="@menu/menu1"
app:layout_constraintBottom_toBottomOf="parent" />
<ImageView
android:id="@+id/chatButton"
android:layout_width="57dp"
android:layout_height="60dp"
android:layout_marginBottom="36dp"
app:layout_constraintBottom_toTopOf="@+id/bottomMenu"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.909"
app:layout_constraintStart_toStartOf="parent"
android:src="@drawable/ic_baseline_chat_24"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
I have try other activity and fragment,it works successful, I trust that the problem is at the second xml file, but for the reference I post the both. Please help me as soon as posible, need use in time.
This is the logcat
2021-11-15 19:16:12.868 30586-30586/com.example.finalyearproject D/AndroidRuntime: Shutting down VM
2021-11-15 19:16:12.870 30586-30586/com.example.finalyearproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.finalyearproject, PID: 30586
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.finalyearproject/com.example.finalyearproject.custMainMenu}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.Button
at com.example.finalyearproject.custMainMenu.onCreate(custMainMenu.kt:19)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
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:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)