[英]Error: have you declared this activity in your AndroidManifest.xml? when trying to intent a class
Here's my code for clicking the button from my class showInforActivity :这是我单击类showInforActivity 中的按钮的代码:
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(showInfoActivity.this, InformationFragment.class);
startActivity(intent);
}
});
Whenever I click the button I got error 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?
Unable to find explicit activity class {com.example.drawerapplication/com.example.drawerapplication.ui.information.InformationFragment}; have you declared this activity in your AndroidManifest.xml?
and it loads the previous class and layout instead of loading this InformationFragment class:它加载以前的类和布局,而不是加载这个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();
}
}
The layout of this InformationFragment is fragment_information :这个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">
...
Heres my **AndroidManifest file:这是我的 **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>
I tried adding InformationFragment
there however, it says Class referenced in the manifest, com.example.drawerapplication.InformationFragment, was not found in the project or the libraries
我尝试在那里添加
InformationFragment
但是,它说Class referenced in the manifest, com.example.drawerapplication.InformationFragment, was not found in the project or the libraries
Please let me know what I'm missing or if you need more information.如果您需要更多信息,请告诉我我缺少什么。 Thanks!
谢谢!
You cannot use an Intent
to start/create a Fragment
much like you would for an Activity
.您不能像使用
Activity
那样使用Intent
来启动/创建Fragment
。 A Fragment
actually lives inside an Activity
ie its lifecycle is bound to an Activity
. Fragment
实际上存在于Activity
即它的生命周期绑定到Activity
。 To start a Fragment
you have to use the FragmentManager
that is available with the Activity
instance要启动
Fragment
您必须使用Activity
实例可用的FragmentManager
This error is coming up because Android thinks that your InformationFragment
is an Activity
rather than a Fragment
and hence it is asking you if you have declared it in your AndroidManifest.xml
file because as the rule says, you need to have each Activity
in your app declared inside the AndroidManifest.xml
file出现此错误是因为 Android 认为您的
InformationFragment
是一个Activity
而不是Fragment
,因此它询问您是否已在AndroidManifest.xml
文件中声明它,因为正如规则所说,您需要在应用程序中拥有每个Activity
在AndroidManifest.xml
文件中声明
So, now you can use something like this , inside your showInfoActivity .所以,现在你可以使用像这样,你showInfoActivity内。 Technically there are many solutions available as to what you could essentially do and that's why I linked you to all possible solutions rather than just writing them here
从技术上讲,关于您基本上可以做什么,有许多解决方案可用,这就是为什么我将您与所有可能的解决方案联系起来,而不是仅仅在这里写它们
You can not use Intent
for navigate to Fragment
from a Activity
.您不能使用
Intent
从Activity
导航到Fragment
。 For this reason you are getting this error.因此,您会收到此错误。 As, Intent receive 2 parameter
Intent(FromActivity, ToActivity.class)
.作为, Intent 接收 2 个参数
Intent(FromActivity, ToActivity.class)
。 you are using fragment in 2nd parameter.您在第二个参数中使用片段。 We know that, when we create a activity then it it declared with
AndroidManifest.xml
file.我们知道,当我们创建一个活动时,它用
AndroidManifest.xml
文件声明。 Here, in your manifest file there are not any activity called InformationFragment
.在这里,在您的清单文件中没有任何名为
InformationFragment
活动。
You can learn Android Navigation Component to easily communicate with Activity and fragments.你可以学习 Android Navigation Component 来轻松地与 Activity 和 Fragment 进行通信。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.