【问题标题】:Can you Nest "Onclicklisteners" in order to run multiple activities?您可以嵌套“Onclicklisteners”以运行多个活动吗?
【发布时间】:2020-05-21 14:25:55
【问题描述】:

我正在创建这个应用程序,我希望用户单击某个按钮 photoButton 并出现一个新活动,提示用户输入 2 个 edittext(entry) 信息。在这两种情况下我都会收到错误消息:

  1. 当我尝试启动活动并从活动中访问内容时
  2. 当我打开activityforresult...时,

我点击一个按钮本质上触发了这两个活动。返回空值究竟是什么?

尝试使用startactivityforresult方法..

   fun take_pic(){
        val takephotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (takephotoIntent.resolveActivity(this.packageManager) != null) {
            startActivityForResult(takephotoIntent, REQUESTCODE)
        } else {
            Toast.makeText(this, "Unable To access Camera... ", Toast.LENGTH_LONG)
                .show()
        }


    }
    fun display_information() {
        /* Connection to firebase       */
        val currentuser = auth.currentUser!!.email


        /* getting the values of the user*/
        val email_of_user = currentuser



        /* Changing the Displayed text                    */
        welcomemessagename.text = email_of_user
        Date.text = "Date:" + date_formatted
    }

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user_main_hub)
        val storageinfo = FirebaseStorage.getInstance().getReference("/images/")

        auth = FirebaseAuth.getInstance()

        /* displaying basic information for the user comfort.        */
        display_information()

        audioscannerButton.setOnClickListener {
            val action = Intent(this, Recordinit::class.java)
            startActivity(action)
        }

        helpbutton.setOnClickListener {
            val action2 = Intent(this, HelpActivity::class.java)
            startActivity(action2)
        }
        photoButton.setOnClickListener {
            val action3 = Intent(this , nameofphoto::class.java)
            startActivityForResult(action3, REQUESTCODE2 )

        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (REQUESTCODE == requestCode && resultCode == Activity.RESULT_OK) {
            //Compressing the bitmap(image) into a byte[] to match the input of the .putbytes method
            val userimage = data?.extras?.get("data") as Bitmap
            val byteoutput = ByteArrayOutputStream()
            userimage.compress(Bitmap.CompressFormat.JPEG,100 , byteoutput)
            val data = byteoutput.toByteArray()
            //ref to the firebase "bucket" database
            val storageinfo = FirebaseStorage.getInstance().getReference("/Images" )
            //extra data that shows who the images belong to (users)


           storageinfo.putBytes(data)


        }else if (requestCode ==REQUESTCODE2) {
            take_pic()

        }
        else {
            super.onActivityResult(requestCode, resultCode, data)
        }
        }



    }

现在的错误:

    Process: com.example.myapplication, PID: 20616
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
        at com.example.myapplication.User_main_hub$onCreate$3.onClick(User_main_hub.kt:82)
        at android.view.View.performClick(View.java:6618)
        at android.view.View.performClickInternal(View.java:6590)
        at android.view.View.access$3100(View.java:781)
        at android.view.View$PerformClick.run(View.java:25950)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:215)
        at android.app.ActivityThread.main(ActivityThread.java:6952)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
I/Process: Sending signal. PID: 20616 SIG: 9
Process 20616 terminated.

【问题讨论】:

  • 这段代码中到底有什么不符合您的预期?
  • 检查描述,我添加了一些关于到底是什么错误的更多信息,我在android studio中提供了运行选项卡的日志,它显示了错误。@AlexMamo
  • 您正在访问第 82 行 User_main_hub.kt:82 上的 null 内容

标签: android firebase kotlin


【解决方案1】:

我在字里行间阅读,所以不确定,但看起来 metabtnsubmit 不是此 Activity 布局的成员 R.layout.activity_user_main_hub,因此合成 getter 无法找到它并返回 null .

也许您试图在下一个活动“nameofphoto”中设置点击侦听器?您无法从此 Activity 访问其 UI 元素。

假设它是一个允许用户输入文本并选择照片的Activity,那么你需要使用startActivityForResult打开它,让它将数据设置为结果,并在这个Activity的onActivityResult()中响应它。

【讨论】:

  • 你能告诉我它是如何用几行代码工作的吗?
  • 我将尝试编写代码以获得更好的理解..
  • 看起来他们最近彻底改变了他们推荐的这样做的方式。我之前没用过registerForActivityResult
  • 我用了你的方法,还是遇到了同样的问题.....看看更新..
【解决方案2】:

NullPointerException 发生在 onClick 内部,并且表达式 nameofphoto::class 与其他 OnClick 方法不同,因此,nameofphoto 必须是原因,在源代码中您在声明或分配 nameofphoto 时不会显示。

【讨论】:

    猜你喜欢
    • 2017-03-11
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    相关资源
    最近更新 更多