首页 > 解决方案 > 错误:您是否在 AndroidManifest.xml 中声明了此活动?尝试上课时

问题描述

这是我从我的类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?,它会加载上一个类和布局,而不是加载这个 InformationFragment类:

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

请让我知道我缺少什么,或者您是否需要更多信息。谢谢!

标签: javaandroidxmlandroid-studioandroid-manifest

解决方案


您不能像Intent使用. A实际上存在于一个ie 中,即它的生命周期绑定到一个. 要启动,您必须使用实例可用的FragmentActivityFragmentActivityActivityFragmentFragmentManagerActivity

出现此错误是因为 Android 认为您InformationFragment是 aActivity而不是 a Fragment,因此它会询问您是否已在AndroidManifest.xml文件中声明它,因为正如规则所说,您需要在文件Activity中声明应用程序中的每个AndroidManifest.xml

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


推荐阅读