Error: have you declared this activity in your AndroidManifest.xml? when trying to intent a class

Error: have you declared this activity in your AndroidManifest.xml? when trying to intent a class

这是我的代码,用于单击 class showInforActivity:

中的按钮
btnRegister.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(showInfoActivity.this, InformationFragment.class);
                    startActivity(intent);
                }
            });

每当我单击按钮时,我都会收到错误 Unable to find explicit activity class {com.example.drawerapplication/com.example.drawerapplication.ui.information.InformationFragment}; have you declared this activity in your AndroidManifest.xml?,它会加载之前的 class 和布局,而不是加载这个 InformationFragment class:

package com.example.drawerapplication.ui.information;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.example.drawerapplication.databinding.FragmentInformationBinding;

public class InformationFragment extends Fragment {

    private FragmentInformationBinding binding;

    private EditText name, address, age, contact;
    private Button btnAdd, btnViewData;
    DatabaseHelper mDatabaseHelper;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentInformationBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        name = binding.name;
        address = binding.address;
        age = binding.age;
        contact = binding.contact;

        btnAdd = binding.add;
        btnViewData = binding.viewdata;

        mDatabaseHelper = new DatabaseHelper(getActivity());

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (name.length() != 0 && address.length() != 0
                && age.length() != 0 && contact.length() != 0){

                    mDatabaseHelper.addData(name.getText().toString(), address.getText().toString(),
                            age.getText().toString().trim(), Long.valueOf(contact.getText().toString().trim()));
                    toastMessages("Data Successfully Inserted!");

                }else {
                    toastMessages("Please complete all the requirements needed");
                }

            }
        });

        btnViewData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), ListDataActivity.class);
                startActivity(intent);
            }
        });

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    private void toastMessages(String message){
        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
    }

}

这个InformationFragment的布局是fragment_information:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".ui.information.InformationFragment"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp">
...

这是我的 **AndroidManifest 文件:

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

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />

    <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/Theme.DrawerApplication">
        <activity android:name=".ui.information.ListDataActivity"/>
        <activity android:name=".ui.information.showInfoActivity"/>
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.DrawerApplication.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity> 
    </application>

</manifest>

我尝试在那里添加 InformationFragment 但是,它显示 Class referenced in the manifest, com.example.drawerapplication.InformationFragment, was not found in the project or the libraries

如果您需要更多信息,请告诉我我遗漏了什么。谢谢!

您不能像使用 Activity 那样使用 Intent 到 start/create 和 FragmentFragment 实际上存在于 Activity 中,即它的生命周期绑定到 Activity。要启动 Fragment,您必须使用 Activity 实例

提供的 FragmentManager

出现此错误是因为 Android 认为您的 InformationFragmentActivity 而不是 Fragment 因此它会询问您是否已声明它在您的 AndroidManifest.xml 文件中,因为按照规则,您需要在 AndroidManifest.xml 文件

中声明应用中的每个 Activity

所以,现在您可以在 showInfoActivity 中使用 this 之类的东西。从技术上讲,有许多解决方案可用于您基本上可以做什么,这就是为什么我将您链接到所有可能的解决方案,而不是只写在这里

您不能使用 IntentActivity 导航到 Fragment。 因此,您会收到此错误。如,Intent接收2个参数Intent(FromActivity, ToActivity.class) 。您在第二个参数中使用片段。我们知道,当我们创建一个 activity 然后它用 AndroidManifest.xml 文件声明。在这里,在您的清单文件中没有任何 activity 称为 InformationFragment

您可以学习 Android 导航组件,以便轻松地与 Activity 和片段进行通信。