更改活动时程序突然结束

Posted

技术标签:

【中文标题】更改活动时程序突然结束【英文标题】:The program suddently finish when changing activity 【发布时间】:2021-12-26 13:14:39 【问题描述】:

遇到切换activity时无法启动新activity的情况,模拟器自行重启程序。

这是切换前的活动

 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()
                    
                

                

这是它的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_
    android:layout_
    tools:context=".RegisterAndLogin.CustLoginActivity"
    android:background="@color/white">

    <FrameLayout
        android:id="@+id/custLogin_header"
        android:layout_
        android:layout_
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        >
        <ImageView
            android:layout_
            android:layout_
            android:contentDescription="image"
            android:src="@drawable/header"
            android:scaleType="fitXY"/>

    </FrameLayout>
    <EditText
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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>

这是切换后的activity

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)
        
    

这是第二个活动的 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:layout_
    android:layout_
    tools:context=".custMainMenu"
    >


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/productRView"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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>

我尝试了其他活动和片段,它成功运行,我相信问题出在第二个 xml 文件中,但为了参考,我将两者都发布了。请尽快帮助我,需要及时使用。

这是日志猫

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 ComponentInfocom.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) 

【问题讨论】:

请同时分享崩溃日志 这能回答你的问题吗? Unfortunately MyApp has stopped. How can I solve this? 请将 Logcat 详细信息连同您的问题一起发布 【参考方案1】:

你有 id 为 chatButton 的 imageView。

<ImageView
        android:id="@+id/chatButton"..

但您将其声明为 Button

private lateinit var chatButton: Button

当然应该是ImageView

【讨论】:

是的,这是一个问题,但问题不是主要问题。项目还是不行 这是基于您的日志的问题。如果您有其他问题,则不会显示在此日志中;此修复解决了您在此处显示的错误投射问题。

以上是关于更改活动时程序突然结束的主要内容,如果未能解决你的问题,请参考以下文章

布尔值不能更改

视频结束时调用下一个活动

每次分析结束时如何更改文本文件特定部分的值?

随时结束程序

linux 当java进程结束后使其重新运行

C程序的执行速度突然提高