Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无原因自动返回上一个活动屏幕_Android_Kotlin_Firebase Realtime Database - Fatal编程技术网

Android 无原因自动返回上一个活动屏幕

Android 无原因自动返回上一个活动屏幕,android,kotlin,firebase-realtime-database,Android,Kotlin,Firebase Realtime Database,我有一个活动叫做LoanActivity。我从MapsActivity转到这个活动。但是,在LoanActivity上运行几秒钟后,它会返回到MapsActivity,而我什么也不做。我需要修复它,但我不确定它为什么会发生 我正在尝试更新以下数据库: Locker对象使其在此处不可用(其唯一id已从MapsActivity传递到此处) 将储物柜的名称、纬度和经度添加到数据库中的Loan对象中 我正在复制下面的代码: class LoanActivity : AppCompatActivity()

我有一个活动叫做
LoanActivity
。我从MapsActivity转到这个活动。但是,在
LoanActivity
上运行几秒钟后,它会返回到
MapsActivity
,而我什么也不做。我需要修复它,但我不确定它为什么会发生

我正在尝试更新以下数据库:

  • Locker对象使其在此处不可用(其唯一id已从
    MapsActivity
    传递到此处)
  • 将储物柜的名称、纬度和经度添加到数据库中的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.project1d/EGL_仿真:eglMakeCurrent:0xe0a85600:ver 3 0(tinfo 0xe0a833d0) 2020-02-11 22:35:30.221 10084-10542/android.bignerdranch.project1d/EGL_仿真:eglCreateContext:0xc436c980:maj 1 min 0 rcv 1 2020-02-11 22:35:31.055 10084-10542/android.bignerdranch.project1d/EGL\u仿真:eglMakeCurrent:0xc436c980:Ver10(tinfo 0xc5fa8210) 2020-02-11 22:35:31.086 10084-10084/android.bignerdranch.project1我/编舞:跳过了56帧!应用程序可能在其主线程上做了太多工作。 2020-02-11 22:35:31.097 10084-10127/android.bignerdranch.project1 I/OpenGLRenderer:Davey!持续时间=948ms;Flags=0,IntentendedVsync=31654948913988,Vsync=31655882247284,OldestInputEvent=9223372036854775807,NewestInputEvent=0,HandleInputStart=31655888412820,AnimationStart=31655888480620,PerformTraversalsStart=31655888514520,DrawStart=316558858120,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\u timerslock\n写入失败:不允许操作 2020-02-11 22:35:36.917 10084-10127/android.bignerdranch.project1d/EGL_仿真:eglMakeCurrent:0xe0a85600:ver 3 0(tinfo 0xe0a833d0) 2020-02-11 22:36:00.230 10084-10542/android.bignerdranch.project1d/EGL\u仿真:eglMakeCurrent:0xc436c980:Ver10(tinfo 0xc5fa8210) 2020-02-11 22:36:01.110 10084-10084/android.bignerdranch.project1 E/SchedPolicy:set\u timerslock\n写入失败:不允许操作
    2020-02-11 22:36:01.157 10084-10127/android.bignerdranch.project1d/EGL_仿真:eglMakeCurrent:0xe0a85600:ver 3 0(tinfo 0xe0a833d0)

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


    希望这能对您有所帮助。

    日志中没有例外:我有这个:2020-02-11 22:35:30.211 10084-10127/android.bignerdranch.project1 D/EGL_仿真:eglMakeCurrent:0xe0a85600:ver 3 0(tinfo 0xe0a833d0)2020-02-11 22:35:30.221 10084-10542/android.bignerdranch.project1 D/EGL_仿真:eglCreateContext:0xc436c980:maj 1 min 0 rcv 1 2020-02-11 22:35:31.055 10084-10542/android.bignerdranch.project1 D/EGL_仿真:eglmakeccurrent:0xc436c980:ver1 0(tinfo 0xc5fa8210)2020-02-11 22:35:31.086 10084-10084/android.bignerdranch.project1我/编舞:跳过了56帧!应用程序可能在其主线程上做了太多工作。2020-02-11 22:35:31.097 10084-10127/android.bignerdranch.project1 I/OpenGLRenderer:Davey


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

    您在Logcat中看到任何异常吗?在某些设备上,应用程序在崩溃时不会关闭,它会重新启动。logcat中没有异常:我添加了logcat,没有异常。我如何打破这个循环?重写数据更改(数据:DataSnapshot){data.children.forEach{child->val工具=child.getValue(工具::class.java)