簡體   English   中英

錯誤:您是否在 AndroidManifest.xml 中聲明了此活動? 當試圖意圖上課時

[英]Error: have you declared this activity in your AndroidManifest.xml? when trying to intent a 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? 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

如果您需要更多信息,請告訴我我缺少什么。 謝謝!

您不能像使用Activity那樣使用Intent來啟動/創建Fragment Fragment實際上存在於Activity即它的生命周期綁定到Activity 要啟動Fragment您必須使用Activity實例可用的FragmentManager

出現此錯誤是因為 Android 認為您的InformationFragment是一個Activity而不是Fragment ,因此它詢問您是否已在AndroidManifest.xml文件中聲明它,因為正如規則所說,您需要在應用程序中擁有每個ActivityAndroidManifest.xml文件中聲明

所以,現在你可以使用像這樣,你showInfoActivity內。 從技術上講,關於您基本上可以做什么,有許多解決方案可用,這就是為什么我將您與所有可能的解決方案聯系起來,而不是僅僅在這里寫它們

您不能使用IntentActivity導航到Fragment 因此,您會收到此錯誤。 作為, Intent 接收 2 個參數Intent(FromActivity, ToActivity.class) 您在第二個參數中使用片段。 我們知道,當我們創建一個活動時,它用AndroidManifest.xml文件聲明。 在這里,在您的清單文件中沒有任何名為InformationFragment活動。

你可以學習 Android Navigation Component 來輕松地與 Activity 和 Fragment 進行通信。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM