ActivityNotFoundException {com.example.greetingcard/androidx.fragment.app.FragmentActivity};

ActivityNotFoundException {com.example.greetingcard/androidx.fragment.app.FragmentActivity};

提问人:tanu 提问时间:7/31/2023 最后编辑:tanu 更新时间:7/31/2023 访问量:73

问:

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()
    }
}

这是 Fragment 活动。

<activity android:name=".fragments.FragmentActivity">
        </activity>

清单文件中的声明。


        fragmentActivityButton.setOnClickListener {
            var intent = Intent(this@MainActivity, FragmentActivity::class.java)
            startActivity(intent)
        }

这是在 MainActivity 中。(startActivity(intent) 引发异常。

<?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>

这是 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'
}

这是 build.gradle 文件。

<?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>

这是 FragmentActivity xml。

找不到异常背后的问题。提前致谢。

应用程序崩溃,导致未找到活动异常。

人造人 android-fragments android-intent 启动活动 ActivityNotFoundException

评论

0赞 Stappo 7/31/2023
请添加所有清单代码、build.gradle 代码、布局 FragmentCtivity 代码和启动 fragment Activity 的活动
0赞 tanu 7/31/2023
嗨,是的,添加了。请查看此内容。谢谢。
0赞 Stappo 7/31/2023
我回复你,请检查我的答案是否正确
0赞 tanu 7/31/2023
是的,你是对的,它奏效了。谢谢sm:)

答:

0赞 Stappo 7/31/2023 #1

经过多次尝试,我发现了问题所在。

在 MainActivity 中,您导入了不正确的 FragmentActivity,将第二个 Activity 命名为“FragmentActivity”,但 FragmentActivity 是 androidx 库的 Android 默认 Activity。

解决方案非常简单:导入正确的活动!

从 MainActivity 中删除

import androidx.fragment.app.FragmentActivity

并添加

import com.example.greetingcard.fragments.FragmentActivity