class FragmentActivity : AppCompatActivity() {
lateinit var binding : ActivityFragmentBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityFragmentBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.fragment1Button.setOnClickListener {
replaceFragment(BlankFragment2())
}
binding.fragment2Button.setOnClickListener {
replaceFragment(BlankFragment2())
}
}
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragment_container_view,fragment)
fragmentTransaction.commit()
}
}
This is the Fragment activity.
<activity android:name=".fragments.FragmentActivity">
</activity>
Declaration in the manifest file.
fragmentActivityButton.setOnClickListener {
var intent = Intent(this@MainActivity, FragmentActivity::class.java)
startActivity(intent)
}
This is in the MainActivity.(startActivity(intent) is throwing the exception.)
<?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"
android:background="@drawable/button_shape"
tools:context=".MainActivity">
<Button
android:id="@+id/goToSecondButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_shape"
android:backgroundTint="@null"
android:text="Go to second Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/childLinearLayout" />
<Button
android:id="@+id/recyclerViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_shape"
android:backgroundTint="@null"
android:text="go to recycler activity"
app:layout_constraintBottom_toTopOf="@+id/fragmentButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5" />
<Button
android:id="@+id/fragmentButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_shape"
android:backgroundTint="@null"
android:text="go to fragment activity"
app:layout_constraintBottom_toTopOf="@+id/text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/recyclerViewButton"
app:layout_constraintVertical_bias="0.5" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="Have a good day!"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/childLinearLayout"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text">
<Button
android:id="@+id/resetButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:baselineAligned="false"
android:text="reset"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/changeButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:baselineAligned="false"
android:text="change"
app:layout_constraintEnd_toEndOf="parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
This is mainactivity xml.
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.greetingcard'
compileSdk 33
defaultConfig {
applicationId "com.example.greetingcard"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures{
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
This is build.gradle file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".fragments.FragmentActivity">
<androidx.fragment.app.FragmentContainerView
android:layout_width="match_parent"
android:layout_height="600dp"
android:id="@+id/fragment_container_view"
android:name="com.example.greetingcard.fragments.BlankFragment" />
<Button
android:layout_width="150dp"
android:layout_height="60dp"
android:id="@+id/fragment1_button"
android:backgroundTint="@color/black"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:layout_marginStart="30dp"
android:text="Fragment 1"/>
<Button
android:layout_width="150dp"
android:layout_height="60dp"
android:id="@+id/fragment2_button"
android:backgroundTint="@color/black"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:layout_marginEnd="30dp"
android:text="Fragment 1"/>
</RelativeLayout>
This is FragmentActivity xml.
Couldn't find the problem behind the exception. Thanks in advance.
App is crashing giving activity not found exception.