Kotlin Instagram克隆。单击按钮时,为什么可以';我不能回到前面的片段吗?
我真的需要你的帮助。 我正在制作一个Instagram克隆,我正在使用片段,我有一个用于个人资料页面。其中,我有一个进入AccountSettingsActivity的按钮,您可以在其中编辑用户信息,我有一个按钮保存更改,另一个按钮返回配置文件片段,但当我使用后者时,应用程序崩溃,出现以下消息:Kotlin Instagram克隆。单击按钮时,为什么可以';我不能回到前面的片段吗?,kotlin,button,fragment,Kotlin,Button,Fragment,我真的需要你的帮助。 我正在制作一个Instagram克隆,我正在使用片段,我有一个用于个人资料页面。其中,我有一个进入AccountSettingsActivity的按钮,您可以在其中编辑用户信息,我有一个按钮保存更改,另一个按钮返回配置文件片段,但当我使用后者时,应用程序崩溃,出现以下消息: 找不到显式活动类 {academy.epicprogramming.kynoanimalrescue/academy.epicprogramming.kynoanimalrescue.fragments
找不到显式活动类 {academy.epicprogramming.kynoanimalrescue/academy.epicprogramming.kynoanimalrescue.fragments.ProfileFragment}; 您是否在AndroidManifest.xml中声明了此活动 这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="academy.epicprogramming.kynoanimalrescue">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!-- <activity-->
<!-- android:name=".Main2Activity"-->
<!-- android:label="@string/title_activity_main2"></activity>-->
<activity android:name=".AccountSettingsActivity" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBYtQl--ajXZy1uzOZoXSNGJLCeyAHUV9s" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".Login.SignUpActivity" />
<activity android:name=".Login.SignInActivity" />
<activity android:name=".Intro.IntroSlider" />
<activity android:name=".MainActivity" />
<activity
android:name=".Intro.SplashScreen"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat"/> <!-- optional (needed if default theme has no action bar) -->
</application>
</manifest>
这是完整的AccountSettingsActivity文件
class AccountSettingsActivity : AppCompatActivity() {
private lateinit var firebaseUser: FirebaseUser
private var checker = ""
private var myUrl = ""
private var imageUri: Uri? = null
private var storageProfilePictureRef: StorageReference? = null
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_account_settings)
firebaseUser = FirebaseAuth.getInstance().currentUser!!
storageProfilePictureRef =
FirebaseStorage.getInstance().reference.child("Profile Pictures")
logout_btn.setOnClickListener {
FirebaseAuth.getInstance().signOut()
val intent = Intent(this@AccountSettingsActivity, ProfileFragment::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
}
close_profile_btn.setOnClickListener {
val intent = Intent(this@AccountSettingsActivity, ProfileFragment::class.java)
startActivity(intent)
finish()
}
change_image_text_btn.setOnClickListener {
checker = "clicked"
CropImage.activity()
.setAspectRatio(1, 1)
.start(this@AccountSettingsActivity)
}
save_info_profile_btn.setOnClickListener {
if (checker == "clicked") {
updateImageAndInfo()
} else {
updateUserInfoOnly()
}
}
userInfo()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null)
{
val result = CropImage.getActivityResult(data)
imageUri = result.uri
profile_image_view_profile_frag.setImageURI(imageUri)
}
}
private fun updateUserInfoOnly() {
when {
full_name_profile_frag.text.toString() == "" -> Toast.makeText(
this,
"Please enter your name",
Toast.LENGTH_LONG
).show()
username_profile_frag.text.toString() == "" -> Toast.makeText(
this,
"Please enter your username",
Toast.LENGTH_LONG
).show()
bio_profile_frag.text.toString() == "" -> {
bio_profile_frag.setText("Estoy usando Kyno animal rescue")
val usersRef =
FirebaseDatabase.getInstance().reference.child("Users")
val userMap = HashMap<String, Any>()
userMap["fullname"] = full_name_profile_frag.text.toString().toLowerCase()
userMap["username"] = username_profile_frag.text.toString()
userMap["bio"] = bio_profile_frag.text.toString()
usersRef.child(firebaseUser.uid).updateChildren(userMap)
Toast.makeText(this, "Account information successfully updated", Toast.LENGTH_LONG)
.show()
val intent = Intent(this@AccountSettingsActivity, MainActivity::class.java)
startActivity(intent)
finish()
}
else -> {
val usersRef =
FirebaseDatabase.getInstance().reference.child("Users")
val userMap = HashMap<String, Any>()
userMap["fullname"] = full_name_profile_frag.text.toString().toLowerCase()
userMap["username"] = username_profile_frag.text.toString()
userMap["bio"] = bio_profile_frag.text.toString()
usersRef.child(firebaseUser.uid).updateChildren(userMap)
Toast.makeText(this, "Account information successfully updated", Toast.LENGTH_LONG)
.show()
val intent = Intent(this@AccountSettingsActivity, MainActivity::class.java)
startActivity(intent)
finish()
}
}
}
private fun userInfo() {
val usersRef =
FirebaseDatabase.getInstance().getReference().child("Users").child(firebaseUser.uid)
usersRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists()) {
val user = dataSnapshot.getValue<User>(User::class.java)
Picasso.get().load(user!!.getImage()).placeholder(R.drawable.profile)
.into(profile_image_view_profile_frag)
full_name_profile_frag.setText(user.getFullname())
username_profile_frag.setText(user.getUsername())
bio_profile_frag.setText(user.getBio())
}
}
override fun onCancelled(p0: DatabaseError) {
}
})
}
private fun updateImageAndInfo() {
when {
imageUri == null -> Toast.makeText(
this,
"Please choose a picture",
Toast.LENGTH_LONG
).show()
full_name_profile_frag.text.toString() == "" -> Toast.makeText(
this,
"Please enter your name",
Toast.LENGTH_LONG
).show()
username_profile_frag.text.toString() == "" -> Toast.makeText(
this,
"Please enter your username",
Toast.LENGTH_LONG
).show()
bio_profile_frag.text.toString() == "" -> {
Toast.makeText(
this,
"Please enter your bio",
Toast.LENGTH_LONG
).show()
}
// CODE TO SET A DEFAULT BIO IF USER DOESN'T ENTER ONE
// bio_profile_frag.text.toString() == "" -> {
// bio_profile_frag.setText("Estoy usando Kyno animal rescue")
//
// val usersRef =
// FirebaseDatabase.getInstance().reference.child("Users")
// val userMap = HashMap<String, Any>()
// userMap["fullname"] = full_name_profile_frag.text.toString().toLowerCase()
// userMap["username"] = username_profile_frag.text.toString()
// userMap["bio"] = bio_profile_frag.text.toString()
//
// usersRef.child(firebaseUser.uid).updateChildren(userMap)
//
// Toast.makeText(this, "Account information successfully updated", Toast.LENGTH_LONG)
// .show()
//
// val intent = Intent(this@AccountSettingsActivity, MainActivity::class.java)
// startActivity(intent)
// finish()
// }
else -> {
val progressDialog = ProgressDialog(this)
progressDialog.setTitle("Account Settings")
progressDialog.setMessage("Please wait while your profile updates")
progressDialog.show()
val fileRef = storageProfilePictureRef!!.child(firebaseUser!!.uid + ".jpg")
var uploadTask: StorageTask<*>
uploadTask = fileRef.putFile(imageUri!!)
uploadTask.continueWithTask(Continuation <UploadTask.TaskSnapshot, Task<Uri>>{ task ->
if (!task.isSuccessful)
{
task.exception?.let {
throw it
progressDialog.dismiss()
}
}
return@Continuation fileRef.downloadUrl
}).addOnCompleteListener (OnCompleteListener<Uri> {task ->
if (task.isSuccessful)
{
val downloadUrl = task.result
myUrl = downloadUrl.toString()
val ref = FirebaseDatabase.getInstance().reference.child("Users")
val userMap = HashMap<String, Any>()
userMap["fullname"] =
full_name_profile_frag.text.toString().toLowerCase()
userMap["username"] = username_profile_frag.text.toString()
userMap["bio"] = bio_profile_frag.text.toString()
userMap["image"] = myUrl
ref.child(firebaseUser.uid).updateChildren(userMap)
Toast.makeText(
this,
"Account information successfully updated",
Toast.LENGTH_LONG
)
.show()
val intent =
Intent(this@AccountSettingsActivity, MainActivity::class.java)
startActivity(intent)
finish()
progressDialog.dismiss()
} else {
progressDialog.dismiss()
}
}
)
}
}
}
class AccountSettingsActivity:AppCompativeActivity(){
私有lateinit var firebaseUser:firebaseUser
private var checker=“”
private var myUrl=“”
私有变量imageUri:Uri?=null
私有变量storageProfilePictureRef:StorageReference?=null
重写创建时的乐趣(savedInstanceState:Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u account\u设置)
firebaseUser=FirebaseAuth.getInstance().currentUser!!
storageProfilePictureRef=
FirebaseStorage.getInstance().reference.child(“配置文件图片”)
注销\u btn.setOnClickListener{
FirebaseAuth.getInstance().signOut()
val intent=intent(this@AccountSettingsActivity,ProfileFragment::class.java)
intent.addFlags(intent.FLAG\活动\清除\任务或intent.FLAG\活动\新任务)
星触觉(意图)
完成()
}
关闭\u profile\u btn.setOnClickListener{
val intent=intent(this@AccountSettingsActivity,ProfileFragment::class.java)
星触觉(意图)
完成()
}
更改\u图像\u文本\u btn.setOnClickListener{
checker=“单击”
CropImage.activity()
.setAspectRatio(1,1)
.开始(this@AccountSettingsActivity)
}
保存\u信息\u配置文件\u btn.setOnClickListener{
如果(检查程序==“单击”){
updateImageAndInfo()
}否则{
UpdateUserInfo仅()
}
}
userInfo()
}
重写activityResult(请求代码:Int,结果代码:Int,数据:Intent?){
super.onActivityResult(请求代码、结果代码、数据)
if(requestCode==CropImage.CROP\u IMAGE\u ACTIVITY\u REQUEST\u CODE&&resultCode==ACTIVITY.RESULT\u OK&&data!=null)
{
val结果=CropImage.getActivityResult(数据)
imageUri=result.uri
配置文件\图像\视图\配置文件\框架setImageURI(imageUri)
}
}
private fun UpdateUserInfo Only(){
什么时候{
全文\u名称\u配置文件\u frag.text.toString()==“”->Toast.makeText(
这
“请输入您的姓名”,
吐司,长度
).show()
username\u profile\u frag.text.toString()==“”->Toast.makeText(
这
“请输入您的用户名”,
吐司,长度
).show()
bio_profile_frag.text.toString()==“”->{
bio_profile_frag.setText(“Estoy usando Kyno动物救援”)
val usersRef=
FirebaseDatabase.getInstance().reference.child(“用户”)
val userMap=HashMap()
userMap[“fullname”]=完整的\u name\u profile\u frag.text.toString().toLowerCase()
userMap[“username”]=用户名\u配置文件\u frag.text.toString()
userMap[“bio”]=bio_profile_frag.text.toString()
usersRef.child(firebaseUser.uid).updateChildren(userMap)
Toast.makeText(此“帐户信息已成功更新”,Toast.LENGTH\u LONG)
.show()
val intent=intent(this@AccountSettingsActivity,MainActivity::class.java)
星触觉(意图)
完成()
}
其他->{
val usersRef=
FirebaseDatabase.getInstance().reference.child(“用户”)
val userMap=HashMap()
userMap[“fullname”]=完整的\u name\u profile\u frag.text.toString().toLowerCase()
userMap[“username”]=用户名\u配置文件\u frag.text.toString()
userMap[“bio”]=bio_profile_frag.text.toString()
usersRef.child(firebaseUser.uid).updateChildren(userMap)
Toast.makeText(此“帐户信息已成功更新”,Toast.LENGTH\u LONG)
.show()
val intent=intent(this@AccountSettingsActivity,MainActivity::class.java)
星触觉(意图)
完成()
}
}
}
私人娱乐用户信息(){
val usersRef=
FirebaseDatabase.getInstance().getReference().child(“用户”).child(firebaseUser.uid)
usersRef.addValueEventListener(对象:ValueEventListener{
覆盖数据更改(dataSnapshot:dataSnapshot){
if(dataSnapshot.exists()){
val user=dataSnapshot.getValue(user::class.java)
Picasso.get().load(user!!.getImage()).placeholder(R.drawable.profile)
.into(纵断面\图像\视图\纵断面\框架)
完整\u名称\u配置文件\u frag.setText(user.getFullname())
username\u profile\u frag.setText(user.getUsername())
bio_profile_frag.setText(user.getBio())
}
}
已取消覆盖乐趣(p0:DatabaseError){
}
})
}
私人娱乐更新页面和信息(){
什么时候{
imageUri==null->Toast.makeText(
这
“请选择一张图片”,
吐司,长度
).show()
全文\u名称\u配置文件\u frag.text.toString()==“”->Toast.makeText(
这
“请输入您的姓名”,
吐司,长度
).show()
username\u profile\u frag.text.toString()==“”->Toast.makeText(
这
“请输入您的用户名”,
吐司,长度
).show()
bio_剖面图
class AccountSettingsActivity : AppCompatActivity() {
private lateinit var firebaseUser: FirebaseUser
private var checker = ""
private var myUrl = ""
private var imageUri: Uri? = null
private var storageProfilePictureRef: StorageReference? = null
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_account_settings)
firebaseUser = FirebaseAuth.getInstance().currentUser!!
storageProfilePictureRef =
FirebaseStorage.getInstance().reference.child("Profile Pictures")
logout_btn.setOnClickListener {
FirebaseAuth.getInstance().signOut()
val intent = Intent(this@AccountSettingsActivity, ProfileFragment::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
}
close_profile_btn.setOnClickListener {
val intent = Intent(this@AccountSettingsActivity, ProfileFragment::class.java)
startActivity(intent)
finish()
}
change_image_text_btn.setOnClickListener {
checker = "clicked"
CropImage.activity()
.setAspectRatio(1, 1)
.start(this@AccountSettingsActivity)
}
save_info_profile_btn.setOnClickListener {
if (checker == "clicked") {
updateImageAndInfo()
} else {
updateUserInfoOnly()
}
}
userInfo()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null)
{
val result = CropImage.getActivityResult(data)
imageUri = result.uri
profile_image_view_profile_frag.setImageURI(imageUri)
}
}
private fun updateUserInfoOnly() {
when {
full_name_profile_frag.text.toString() == "" -> Toast.makeText(
this,
"Please enter your name",
Toast.LENGTH_LONG
).show()
username_profile_frag.text.toString() == "" -> Toast.makeText(
this,
"Please enter your username",
Toast.LENGTH_LONG
).show()
bio_profile_frag.text.toString() == "" -> {
bio_profile_frag.setText("Estoy usando Kyno animal rescue")
val usersRef =
FirebaseDatabase.getInstance().reference.child("Users")
val userMap = HashMap<String, Any>()
userMap["fullname"] = full_name_profile_frag.text.toString().toLowerCase()
userMap["username"] = username_profile_frag.text.toString()
userMap["bio"] = bio_profile_frag.text.toString()
usersRef.child(firebaseUser.uid).updateChildren(userMap)
Toast.makeText(this, "Account information successfully updated", Toast.LENGTH_LONG)
.show()
val intent = Intent(this@AccountSettingsActivity, MainActivity::class.java)
startActivity(intent)
finish()
}
else -> {
val usersRef =
FirebaseDatabase.getInstance().reference.child("Users")
val userMap = HashMap<String, Any>()
userMap["fullname"] = full_name_profile_frag.text.toString().toLowerCase()
userMap["username"] = username_profile_frag.text.toString()
userMap["bio"] = bio_profile_frag.text.toString()
usersRef.child(firebaseUser.uid).updateChildren(userMap)
Toast.makeText(this, "Account information successfully updated", Toast.LENGTH_LONG)
.show()
val intent = Intent(this@AccountSettingsActivity, MainActivity::class.java)
startActivity(intent)
finish()
}
}
}
private fun userInfo() {
val usersRef =
FirebaseDatabase.getInstance().getReference().child("Users").child(firebaseUser.uid)
usersRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists()) {
val user = dataSnapshot.getValue<User>(User::class.java)
Picasso.get().load(user!!.getImage()).placeholder(R.drawable.profile)
.into(profile_image_view_profile_frag)
full_name_profile_frag.setText(user.getFullname())
username_profile_frag.setText(user.getUsername())
bio_profile_frag.setText(user.getBio())
}
}
override fun onCancelled(p0: DatabaseError) {
}
})
}
private fun updateImageAndInfo() {
when {
imageUri == null -> Toast.makeText(
this,
"Please choose a picture",
Toast.LENGTH_LONG
).show()
full_name_profile_frag.text.toString() == "" -> Toast.makeText(
this,
"Please enter your name",
Toast.LENGTH_LONG
).show()
username_profile_frag.text.toString() == "" -> Toast.makeText(
this,
"Please enter your username",
Toast.LENGTH_LONG
).show()
bio_profile_frag.text.toString() == "" -> {
Toast.makeText(
this,
"Please enter your bio",
Toast.LENGTH_LONG
).show()
}
// CODE TO SET A DEFAULT BIO IF USER DOESN'T ENTER ONE
// bio_profile_frag.text.toString() == "" -> {
// bio_profile_frag.setText("Estoy usando Kyno animal rescue")
//
// val usersRef =
// FirebaseDatabase.getInstance().reference.child("Users")
// val userMap = HashMap<String, Any>()
// userMap["fullname"] = full_name_profile_frag.text.toString().toLowerCase()
// userMap["username"] = username_profile_frag.text.toString()
// userMap["bio"] = bio_profile_frag.text.toString()
//
// usersRef.child(firebaseUser.uid).updateChildren(userMap)
//
// Toast.makeText(this, "Account information successfully updated", Toast.LENGTH_LONG)
// .show()
//
// val intent = Intent(this@AccountSettingsActivity, MainActivity::class.java)
// startActivity(intent)
// finish()
// }
else -> {
val progressDialog = ProgressDialog(this)
progressDialog.setTitle("Account Settings")
progressDialog.setMessage("Please wait while your profile updates")
progressDialog.show()
val fileRef = storageProfilePictureRef!!.child(firebaseUser!!.uid + ".jpg")
var uploadTask: StorageTask<*>
uploadTask = fileRef.putFile(imageUri!!)
uploadTask.continueWithTask(Continuation <UploadTask.TaskSnapshot, Task<Uri>>{ task ->
if (!task.isSuccessful)
{
task.exception?.let {
throw it
progressDialog.dismiss()
}
}
return@Continuation fileRef.downloadUrl
}).addOnCompleteListener (OnCompleteListener<Uri> {task ->
if (task.isSuccessful)
{
val downloadUrl = task.result
myUrl = downloadUrl.toString()
val ref = FirebaseDatabase.getInstance().reference.child("Users")
val userMap = HashMap<String, Any>()
userMap["fullname"] =
full_name_profile_frag.text.toString().toLowerCase()
userMap["username"] = username_profile_frag.text.toString()
userMap["bio"] = bio_profile_frag.text.toString()
userMap["image"] = myUrl
ref.child(firebaseUser.uid).updateChildren(userMap)
Toast.makeText(
this,
"Account information successfully updated",
Toast.LENGTH_LONG
)
.show()
val intent =
Intent(this@AccountSettingsActivity, MainActivity::class.java)
startActivity(intent)
finish()
progressDialog.dismiss()
} else {
progressDialog.dismiss()
}
}
)
}
}
}
supportFragmentManager
.beginTransaction()
.add(R.id.root_layout, ProfileFragment.newInstance(), "")
.commit()