gpt4 book ai didi

android - 您可以嵌套 “Onclicklisteners”来运行多个 Activity 吗?

转载 作者:行者123 更新时间:2023-12-02 12:50:54 25 4
gpt4 key购买 nike

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

  • 当我尝试启动 Activity 并从 Activity
  • 中访问内容时
  • 当我打开 Activity 结果...时,

  • 我单击一个按钮本质上会触发这两个 Activity 。返回空值到底是什么?

    尝试使用 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.

    最佳答案

    我正在阅读两行之间的内容,所以不确定,但是metabtnsubmit似乎不是此Activity布局的成员R.layout.activity_user_main_hub,因此合成的 setter/getter 无法找到它并返回null

    也许您正在尝试在下一个 Activity “nameofphoto”中设置点击监听器?您无法通过此 Activity 访问其UI元素。

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

    关于android - 您可以嵌套 “Onclicklisteners”来运行多个 Activity 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61936742/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com