Skip to content
Advertisement

Kotlin App open activity 2 with button CRASH

so this is the error in the logcat however when i try to declare “CreateNoteFragment” in the manifest only the .MainActivity & .SplashScreen appear and does not let me declare the .CreateNoteFragment

my CreateNoteFragment also references “BaseFragment” activity as such below error. my button click code is in .MainActivity the only place it wont break and here is the code for that below. when click it crashes the app any help would be appreciated

    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.moradi.moradinotespro/com.moradi.moradinotespro.CreateNoteFragment}; have you declared this activity in your AndroidManifest.xml?
class CreateNoteFragment : BaseFragment() {

  //Button Click Create Note
        fabBtnCreateNote == findViewById(R.id.fabBtnCreateNote)

        fabBtnCreateNote.setOnClickListener {
            val intent = Intent(this, CreateNoteFragment::class.java)
            startActivity(intent)

        }

        replaceFragment(HomeFragment.newInstance(), true)
    }

manifest included

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.moradi.moradinotespro">

    <!-- Permissions
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    -->

    <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=".MainActivity"/>
            <activity android:name=".SplashScreenActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>
    </application>

</manifest>

Splash

class SplashScreenActivity : AppCompatActivity() {

    lateinit var handler: Handler
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash_screen)

        handler = Handler()
        handler.postDelayed({
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        },  2000)  // delaying for 2 sec till open
        }
    }

CreateNoteFragment Activity

class CreateNoteFragment : BaseFragment() {
    var currentDate:String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_create_note, container, false)
    }

    companion object {
        @JvmStatic
        fun newInstance() =
            CreateNoteFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val sdf = SimpleDateFormat("dd/m/yyyy hh:m:ss")
        currentDate = sdf.format(Date())

        tvDateTime.text = currentDate

        imgDone.setOnClickListener {
            //Save Note
            saveNote()
        }

        imgBack.setOnClickListener {
            replaceFragment(HomeFragment.newInstance(),  false)
        }
    }
    private fun saveNote() {
        if (etNoteTitle.text.isNullOrEmpty()) {
            Toast.makeText(context, "Title Required", Toast.LENGTH_SHORT).show()
        }
        if (etNoteSubTitle.text.isNullOrEmpty()) {
            Toast.makeText(context, "SubTitle Required", Toast.LENGTH_SHORT).show()
        }

        if (etNoteDesc.text.isNullOrEmpty()) {
            Toast.makeText(context, "Note Required", Toast.LENGTH_SHORT).show()
        }



        launch {
        val notes = Notes()
            notes.title = etNoteTitle.text.toString()
            notes.subTitle = etNoteSubTitle.text.toString()
            notes.noteText = etNoteDesc.text.toString()
            notes.dateTime = currentDate
            context?.let {
            NotesDatabase.getDatabase(it).noteDao().insertNotes(notes)
                etNoteTitle.setText("")
                etNoteSubTitle.setText("")
                etNoteDesc.setText("")
            }

        }
    }

    private fun replaceFragment(fragment: Fragment, istransition: Boolean) {
        val fragmentTransition = activity!!.supportFragmentManager.beginTransaction()

        if (istransition) {
            fragmentTransition.setCustomAnimations(
                android.R.anim.slide_out_right,
                android.R.anim.slide_in_left
            )
        }
        fragmentTransition.replace(R.id.frame_layout, fragment)
            .addToBackStack(fragment.javaClass.simpleName)
    }
}

Advertisement

Answer

Problem here:

fabBtnCreateNote.setOnClickListener {
            val intent = Intent(this, CreateNoteFragment::class.java) 
            startActivity(intent)

        }

CreateNoteFragment is not an Activity, so you cannot start it as an activity

First, you need to create main_activity.xml file and sepecific id of Framelayout with “frame_layout” like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>

Then, change set content view for MainActivity with this line:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
}

You need to replace from HomeFragment to CreateNoteFragment:

    fabBtnCreateNote.setOnClickListener {
        val newFragment = CreateNoteFragment.newInstance()
        activity?.let {activity->
            val transaction = activity.supportFragmentManager.beginTransaction()
        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack if needed
           transaction.replace(R.id.fragment_container, newFragment,CreateNoteFragment::class.simpleName);
        // Commit the transaction
           transaction.commit();  
      }
}

Your CreateNoteFragment was added to back stack, so you just call onBackPress() when back button clicked: Change your code:

imgBack.setOnClickListener {
        activity.onBackPress()
    }
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement