我正在创建此应用程序,我希望用户单击某个按钮photoButton
并出现一个新的活动,提示用户输入2个edittext(entry)有价值的信息。在这两种情况下我都遇到错误:
我单击一个按钮本质上触发了两个活动。究竟返回一个空值是什么?
尝试使用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.
答案 0 :(得分:1)
我正在阅读两行之间的内容,所以不确定,但是metabtnsubmit
似乎不是此Activity R.layout.activity_user_main_hub
的布局成员,因此合成的getter找不到它,并且返回null
。
也许您正在尝试在下一个活动“ nameofphoto
”中设置点击监听器?您无法通过此活动访问其UI元素。
假设这是一个活动,允许用户输入文本并选择照片,那么您需要使用startActivityForResult
打开它,将其设置为结果,然后在该活动的{{ 1}}。
答案 1 :(得分:0)
NullPointerException发生在onClick内,表达式nameofphoto :: class与其他OnClick方法是不同的,因此,在声明或分配nameofphoto时,在源代码中您不会显示nameofphoto。 / p>