有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java错误:您是否在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?,它会加载上一个类和布局,而不是加载这个 信息片段类:

package com.example.drawerapplication.ui.information;

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

import 安卓x.annotation.NonNull;
import 安卓x.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();
    }

}

这个信息片段的布局是片段信息

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:orientation="vertical"
    tools:context=".ui.information.InformationFragment"
    安卓:layout_marginStart="10dp"
    安卓:layout_marginEnd="10dp">
...

这是我的**AndroidManifest文件:

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

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

    <application
        安卓:allowBackup="true"
        安卓:icon="@mipmap/ic_launcher"
        安卓:label="@string/app_name"
        安卓:roundIcon="@mipmap/ic_launcher_round"
        安卓:supportsRtl="true"
        安卓:theme="@style/Theme.DrawerApplication">
        <activity 安卓:name=".ui.information.ListDataActivity"/>
        <activity 安卓:name=".ui.information.showInfoActivity"/>
        <activity
            安卓:name=".MainActivity"
            安卓:exported="true"
            安卓:label="@string/app_name"
            安卓:theme="@style/Theme.DrawerApplication.NoActionBar">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.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

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


共 (2) 个答案

  1. # 1 楼答案

    不能使用IntentActivity导航到Fragment。 由于这个原因,你会得到这个错误。作为,意图接收2个参数Intent(FromActivity, ToActivity.class) 。您正在第二个参数中使用片段。我们知道,当我们创建一个活动时,它用AndroidManifest.xml文件声明。这里,在您的清单文件中没有任何名为InformationFragment的活动

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

  2. # 2 楼答案

    不能像使用Activity那样使用Intent来启动/创建Fragment。一个Fragment实际上生活在一个Activity内,即它的生命周期绑定到一个Activity。要启动Fragment,必须使用Activity实例中可用的FragmentManager

    出现这个错误是因为Android认为你的InformationFragment是一个Activity而不是一个Fragment,因此它会问你是否在AndroidManifest.xml文件中声明了它,因为按照规则,你需要在AndroidManifest.xml文件中声明应用程序中的每个Activity

    所以,现在你可以在你的showInfoActivity中使用this之类的东西。从技术上讲,关于你基本上可以做什么,有很多解决方案,这就是为什么我把你和所有可能的解决方案联系起来,而不是把它们写在这里