【问题标题】:Automatically returns to the previous activity screen for no reason无故自动返回上一个活动画面
【发布时间】:2020-05-26 18:14:18
【问题描述】:

我有一个名为LoanActivity 的活动。我从 MapsActivity 去这个活动。但是,在LoanActivity 上几秒钟后,它会弹回MapsActivity,而我没有做任何事情。我需要修复它,但我不确定它为什么会发生。

我正在尝试更新数据库:

  1. Lo​​cker 对象使其在此处不可用(其唯一 ID 已从 MapsActivity 传递到此处),
  2. 将储物柜的名称和经纬度添加到数据库中的 Loan 对象中

我正在复制下面的代码:

class LoanActivity : AppCompatActivity() {

    private lateinit var locationsText: EditText  //to show locker name
    private lateinit var Results: EditText        //to show locker address
    private lateinit var loanLogoutButton: Button

    private lateinit var firebaseDatabase: FirebaseDatabase
    var aTool: Tool? =null
    var thelocker: Locker?=null
    var lockerName = "XXX"
    var lockerAddress = "YYY"
    var id: String? = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_loan)

        firebaseDatabase = FirebaseDatabase.getInstance()

        locationsText = findViewById(R.id.locationsText)
        Results = findViewById(R.id.Results)
        loanLogoutButton= findViewById(R.id.loanLogoutButton)
        loanLogoutButton.isEnabled

        val intent = getIntent()
        //grab the data passed from MapsActivity
        //id of the loan, use this to find other details of it from the database
        id = intent?.getStringExtra("IDofLI")
        //name of the locker is unique, get other details from the database
        val nameOfLocker = intent.getStringExtra("MARKERTITLE")
        val lockerLT = intent.getDoubleExtra("MARKERLT", 0.0)
        val lockerLG = intent.getDoubleExtra("MARKERLG", 0.0)
        doAsync {
            //Find the locker with id, and make it unavailable
            //We will put this loaned item in it
            val reference = firebaseDatabase.getReference("Locker")
            val addValueEventListener =
                reference.addValueEventListener(object : ValueEventListener {

                    override fun onCancelled(error: DatabaseError) {
                        Log.d("LoanA", "3:onCreate")
                        runOnUiThread {
                            Toast.makeText(
                                this@LoanActivity,
                                "Failed to reach database: $error!",
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    }

                    override fun onDataChange(data: DataSnapshot) {
                          data.children.forEach { child ->
                            val locker = child.getValue(Locker::class.java)

                            if (locker != null && locker.name == nameOfLocker) {

                                //This is how you update the database entry
                                var dr = reference.child(child.key.toString())
                                //Create a brand new locker, same as old one, but set available field to 0
                                thelocker = Locker(
                                    locker.name,
                                    locker.day,
                                    locker.month,
                                    locker.year,
                                    0,
                                    locker.zip,
                                    locker.address,
                                    locker.RenterEmail,
                                    locker.LoanerEmail,
                                    locker.rOrl,  //rent or loan
                                    locker.lat,
                                    locker.longt
                                )
                                lockerAddress = locker.address
                                lockerName = locker.name
                                dr.setValue(thelocker)
                                locationsText.setText(lockerName)
                                Results.setText(lockerAddress)
                            }
                        }

                    }

                })

            //Find the Loan item with id
            //Update it in the database to have the lockerName
            val reference2 = firebaseDatabase.getReference("Tool/Loan")
            val addValueEventListener2 =
                reference2.addValueEventListener(object : ValueEventListener {
                    override fun onCancelled(error: DatabaseError) {

                        runOnUiThread {
                            Toast.makeText(
                                this@LoanActivity,
                                "Failed to reach database: $error!",
                                Toast.LENGTH_LONG
                            ).show()
                        }
                    }

                    override fun onDataChange(data: DataSnapshot) {
                         data.children.forEach { child ->
                            val tool = child.getValue(Tool::class.java)

                            if (tool != null && child.key.toString() == id) {
                                Log.d(
                                    "TOOL_FOUND*",
                                    "Item being loaned is updated in DB: id=" + child.key
                                )
                                //This is how you update the database entry
                                var dr = reference2.child(child.key.toString())
                                //create a brand new tool, same as old one, but set lockerName
                                aTool = Tool(
                                    tool.day,
                                    tool.month,
                                    tool.year,
                                    tool.type,
                                    tool.available,
                                    tool.zip,
                                    tool.host,
                                    tool.guest,
                                    tool.rOrl,  //rent or loan
                                    nameOfLocker,
                                    lockerLT,
                                    lockerLG,
                                    tool.price
                                )
                                dr.setValue(aTool)
                            }
                        }
                        Log.d("LoanA", "11")
                    }
                })
            Log.d("LoanA", "12")
        }

        loanLogoutButton.setOnClickListener {
            Log.d("LoanA", "13")
            //send the id of this restaurant to the next screen for reviews
            val intent2 = Intent(this@LoanActivity, MainActivity::class.java)
            startActivity(intent2)
        }
    }
}

logcat 中没有异常: 我有这个: 2020-02-11 22:35:30.211 10084-10127/android.bignerdranch.project1 D/EGL_emulation: eglMakeCurrent: 0xe0a85600: ver 3 0 (tinfo 0xe0a833d0) 2020-02-11 22:35:30.221 10084-10542/android.bignerdranch.project1 D/EGL_emulation: eglCreateContext: 0xc436c980: maj 1 min 0 rcv 1 2020-02-11 22:35:31.055 10084-10542/android.bignerdranch.project1 D/EGL_emulation: eglMakeCurrent: 0xc436c980: ver 1 0 (tinfo 0xc5fa8210) 2020-02-11 22:35:31.086 10084-10084/android.bignerdranch.project1 I/Choreographer:跳过 56 帧!应用程序可能在其主线程上做了太多工作。 2020-02-11 22:35:31.097 10084-10127/android.bignerdranch.project1 I/OpenGLRenderer:戴维!持续时间=948ms;标志= 0,IntendedVsync = 31654948913988,垂直同步= 31655882247284,OldestInputEvent = 9223372036854775807,NewestInputEvent = 0,HandleInputStart = 31655888412820,AnimationStart = 31655888480620,PerformTraversalsStart = 31655888514520,DrawStart = 31655888758120,SyncQueued = 31655888793620,SyncStart = 31655890131720,IssueDrawCommandsStart = 31655890241220,SwapBuffers = 31655893764920,FrameCompleted=31655899112020,DequeueBufferDuration=817000,QueueBufferDuration=1703000, 2020-02-11 22:35:36.905 10084-10084/android.bignerdranch.project1 E/SchedPolicy:set_timerslack_ns 写入失败:不允许操作 2020-02-11 22:35:36.917 10084-10127/android.bignerdranch.project1 D/EGL_emulation: eglMakeCurrent: 0xe0a85600: ver 3 0 (tinfo 0xe0a833d0) 2020-02-11 22:36:00.230 10084-10542/android.bignerdranch.project1 D/EGL_emulation: eglMakeCurrent: 0xc436c980: ver 1 0 (tinfo 0xc5fa8210) 2020-02-11 22:36:01.110 10084-10084/android.bignerdranch.project1 E/SchedPolicy:set_timerslack_ns 写入失败:不允许操作 2020-02-11 22:36:01.157 10084-10127/android.bignerdranch.project1 D/EGL_emulation: eglMakeCurrent: 0xe0a85600: ver 3 0 (tinfo 0xe0a833d0)

【问题讨论】:

  • 您在 Logcat 中看到任何异常吗?在某些设备上,应用程序在崩溃时不会关闭,而是会重新启动。
  • logcat 中没有异常:

标签: android kotlin firebase-realtime-database


【解决方案1】:

您的应用似乎在LoanActivity 上的某个时间后崩溃了,这就是它返回MapsActivity 的原因。当它回到MapsActivity时,请查看您的日志。

希望这会对你有所帮助。

【讨论】:

  • 我已经添加了logcat,没有异常。我如何打破这个循环?覆盖 fun onDataChange(data: DataSnapshot) { data.children.forEach { child -> val tool = child.getValue(Tool::class.java)
【解决方案2】:

logcat 中没有异常:我有这个:2020-02-11 22:35:30.211 10084-10127/android.bignerdranch.project1 D/EGL_emulation: eglMakeCurrent: 0xe0a85600: ver 3 0 (tinfo 0xe0a833d0) 2020-02 -11 22:35:30.221 10084-10542/android.bignerdranch.project1 D/EGL_emulation: eglCreateContext: 0xc436c980: maj 1 min 0 rcv 1 2020-02-11 22:35:31.055 10084-10542/android.bignerdranch.project1 D /EGL_emulation: eglMakeCurrent: 0xc436c980: ver 1 0 (tinfo 0xc5fa8210) 2020-02-11 22:35:31.086 10084-10084/android.bignerdranch.project1 I/Choreographer: 跳过 56 帧!应用程序可能在其主线程上做了太多工作。 2020-02-11 22:35:31.097 10084-10127/android.bignerdranch.project1 I/OpenGLRenderer:戴维!

Logcat 显示您正在 UI 线程上执行一些繁重的任务,这就是应用程序崩溃的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多