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
如果你需要更多信息,请告诉我我遗漏了什么。谢谢
# 1 楼答案
不能使用
Intent
从Activity
导航到Fragment
。 由于这个原因,你会得到这个错误。作为,意图接收2个参数Intent(FromActivity, ToActivity.class)
。您正在第二个参数中使用片段。我们知道,当我们创建一个活动时,它用AndroidManifest.xml
文件声明。这里,在您的清单文件中没有任何名为InformationFragment
的活动您可以学习Android导航组件,以便轻松地与活动和片段进行通信
# 2 楼答案
不能像使用
Activity
那样使用Intent
来启动/创建Fragment
。一个Fragment
实际上生活在一个Activity
内,即它的生命周期绑定到一个Activity
。要启动Fragment
,必须使用Activity
实例中可用的FragmentManager
出现这个错误是因为Android认为你的
InformationFragment
是一个Activity
而不是一个Fragment
,因此它会问你是否在AndroidManifest.xml
文件中声明了它,因为按照规则,你需要在AndroidManifest.xml
文件中声明应用程序中的每个Activity
所以,现在你可以在你的showInfoActivity中使用this之类的东西。从技术上讲,关于你基本上可以做什么,有很多解决方案,这就是为什么我把你和所有可能的解决方案联系起来,而不是把它们写在这里