Home > Software design >  force close while intent from activity to activity
force close while intent from activity to activity

Time:02-02

so i want to intent from settings activity into edit profile activity, but when i clicked the edit profile button in settings activity, suddenly my application is force closed. for the information, i have profile fragment attached to main activity. on profile fragment, it has button that intent to settings activity. but, in settings activity it also has button that intent to edit profile activity. the problem is i can't intent from the settings activity to edit profile activity.

here's the SettingsActivity.kt

class SettingsActivity : AppCompatActivity(), View.OnClickListener {
    private lateinit var settingsBinding: ActivitySettingsBinding
    private var refUsers : DatabaseReference? = null
    private var firebaseUser : FirebaseUser? = null

    companion object{
        fun getLaunchService (from: Context) = Intent(from, SettingsActivity::class.java).apply {
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        settingsBinding = ActivitySettingsBinding.inflate(layoutInflater)
        setContentView(settingsBinding.root)
        supportActionBar?.hide()

        settingsBinding.cvSettingsLogOut.setOnClickListener(this)
        settingsBinding.btnEditProfile.setOnClickListener(this)
        userInfo()

    }

    private fun userInfo(){
        firebaseUser = FirebaseAuth.getInstance().currentUser
        refUsers = FirebaseDatabase.getInstance().getReference("User").child(firebaseUser!!.uid)
        refUsers!!.addValueEventListener(object : ValueEventListener {
            override fun onCancelled(error: DatabaseError) {
                TODO("Not yet implemented")
            }

            override fun onDataChange(snapshot: DataSnapshot) {
                for (p0 in snapshot.children){
                    val userName = snapshot.child("userName").value.toString()
                    val email = snapshot.child("email").value.toString()
                    val profileImage = snapshot.child("profile_image").value.toString()

                    settingsBinding.tvProfileUsername.text = userName
                    settingsBinding.tvProfileEmail.text = email

                    Glide.with(this@SettingsActivity).load(profileImage).into(settingsBinding.ivProfileSettings)
                }
            }
        })
    }

    override fun onClick(v: View) {
        when(v.id){
            R.id.btnEditProfile -> startActivity(EditProfileActivity.getLaunchService(this))
            R.id.cvSettingsLogOut -> logOut()
        }
    }

    private fun logOut() {
        /* Create a new instance of the AlertDialog class. */
        var alertDialog: AlertDialog? = null
        val builder = AlertDialog.Builder(this)
        /* Inflate the layout file for the dialog box. */
        val view = DialogConfirmLogoutBinding.inflate(layoutInflater)
        /* Set the view of the dialog box. */
        builder.setView(view.root)

        view.btnConfirmCancel.setOnClickListener {
            alertDialog?.dismiss()
        }

        view.btnConfirmYes.setOnClickListener {
            alertDialog?.dismiss()
            FirebaseAuth.getInstance().signOut()

            val intent = Intent(this, LoginActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivity(intent)
            finish()
        }
        /* Showing the dialog box. */
        alertDialog = builder.create()
        alertDialog.show()

    }

}

my EditProfileAcitivity.kt

class EditProfileActivity : AppCompatActivity() {
    private lateinit var editProfileBinding: ActivityEditProfileBinding

    companion object{
        fun getLaunchService (from: Context) = Intent(from, EditProfileActivity::class.java).apply {
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        editProfileBinding = ActivityEditProfileBinding.inflate(layoutInflater)
        setContentView(editProfileBinding.root)
        supportActionBar?.hide()

    }
}

here's the layout for EditProfile xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.EditProfileActivity">

    <ImageView
        android:id="@ id/ivBackEditProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_20dp"
        android:src="@drawable/ic_back"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_20dp"
        android:fontFamily="@font/poppins_medium"
        android:text="@string/edit_profile"
        android:textColor="@color/black"
        android:textSize="@dimen/_18sp"
        app:layout_constraintBottom_toBottomOf="@ id/ivBackEditProfile"
        app:layout_constraintStart_toEndOf="@ id/ivBackEditProfile"
        app:layout_constraintTop_toTopOf="@ id/ivBackEditProfile" />

    <LinearLayout
        android:id="@ id/llAddEditProfile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_20dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/ivBackEditProfile">

        <ImageView
            android:id="@ id/ivAddBgEditProfile"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_150dp"
            android:scaleType="fitXY"
            android:src="@drawable/bg_addbgeditprofile">

        </ImageView>

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@ id/ivAddProfilePhoto"
            android:layout_width="@dimen/_100dp"
            android:layout_height="@dimen/_100dp"
            android:layout_marginStart="@dimen/_20dp"
            android:scaleType="fitCenter"
            android:src="@drawable/bg_addprofilephtoeditpro" />
    </LinearLayout>

    <EditText
        android:id="@ id/etNameEditProfile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="@dimen/_25dp"
        android:background="@drawable/bg_et_editprofile"
        android:hint="Name"
        android:layout_marginTop="@dimen/_20dp"
        android:inputType="textPersonName"
        android:maxLines="1"
        android:maxLength="15"
        android:padding="@dimen/_15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/llAddEditProfile" />

    <EditText
        android:id="@ id/etBioEditProfile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_et_editprofile"
        android:hint="Bio"
        android:layout_marginHorizontal="@dimen/_25dp"
        android:maxLines="3"
        android:inputType="textMultiLine"
        android:layout_marginTop="@dimen/_20dp"
        android:maxLength="100"
        android:padding="@dimen/_15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etNameEditProfile" />

    <EditText
        android:id="@ id/etLinkEditProfile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_et_editprofile"
        android:hint="Link"
        android:layout_marginHorizontal="@dimen/_25dp"
        android:maxLines="1"
        android:inputType="textUri"
        android:layout_marginTop="@dimen/_20dp"
        android:maxLength="15"
        android:padding="@dimen/_15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etBioEditProfile" />

    <Button
        android:id="@ id/btnSaveEditProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginVertical="@dimen/_25dp"
        android:layout_marginEnd="@dimen/_25dp"
        android:background="@drawable/bg_btnaddpost"
        android:fontFamily="@font/poppins_semibold"
        android:paddingHorizontal="@dimen/_50dp"
        android:text="Save"
        android:textAllCaps="false"
        android:textSize="@dimen/_18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

this is the log error that i got cuz of sudden force close on my app while intent to editprofile activity. if u know what's goin on with my code, pls help me. Thanks.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.isjieman.ocion, PID: 5334
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.isjieman.ocion/com.isjieman.ocion.activity.EditProfileActivity}: android.view.InflateException: Binary XML file line #55 in com.isjieman.ocion:layout/activity_edit_profile: Binary XML file line #55 in com.isjieman.ocion:layout/activity_edit_profile: Error inflating class <unknown>
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.view.InflateException: Binary XML file line #55 in com.isjieman.ocion:layout/activity_edit_profile: Binary XML file line #55 in com.isjieman.ocion:layout/activity_edit_profile: Error inflating class <unknown>
     Caused by: android.view.InflateException: Binary XML file line #55 in com.isjieman.ocion:layout/activity_edit_profile: Error inflating class <unknown>
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:852)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
        at com.isjieman.ocion.databinding.ActivityEditProfileBinding.inflate(ActivityEditProfileBinding.java:80)
        at com.isjieman.ocion.databinding.ActivityEditProfileBinding.inflate(ActivityEditProfileBinding.java:74)
        at com.isjieman.ocion.activity.EditProfileActivity.onCreate(EditProfileActivity.kt:20)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.IllegalArgumentException: ScaleType FIT_CENTER not supported.
        at de.hdodenhof.circleimageview.CircleImageView.setScaleType(CircleImageView.java:134)
        at android.widget.ImageView.<init>(ImageView.java:223)
        at android.widget.ImageView.<init>(ImageView.java:190)
        at de.hdodenhof.circleimageview.CircleImageView.<init>(CircleImageView.java:98)
        at de.hdodenhof.circleimageview.CircleImageView.<init>(CircleImageView.java:94)
            ... 29 more

CodePudding user response:

You are getting because you have set android:scaleType="fitCenter" in below code and CircleImageView not supporting fitCenter

<de.hdodenhof.circleimageview.CircleImageView
            android:id="@ id/ivAddProfilePhoto"
            android:layout_width="@dimen/_100dp"
            android:layout_height="@dimen/_100dp"
            android:layout_marginStart="@dimen/_20dp"
            android:scaleType="fitCenter"
            android:src="@drawable/bg_addprofilephtoeditpro" />

kindly read limitation of library first https://github.com/hdodenhof/CircleImageView#limitations

  • Related