這是我單擊類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
如果您需要更多資訊,請告訴我我缺少什么。謝謝!
uj5u.com熱心網友回復:
你不能用一個Intent啟動/創建一個Fragment很像你會為一個Activity。AFragment實際上存在于Activityie 中,它的生命周期系結到Activity. 要啟動 a,Fragment您必須使用實體FragmentManager可用的Activity
出現此錯誤是因為 Android 認為您InformationFragment是 anActivity而不是 a Fragment,因此它詢問您是否已在AndroidManifest.xml檔案中宣告它,因為正如規則所說,您需要Activity在應用程式中將每個宣告在AndroidManifest.xml檔案中
所以,現在你可以使用像這樣,你的內部showInfoActivity。從技術上講,關于您基本上可以做什么,有許多解決方案可用,這就是為什么我將您與所有可能的解決方案聯系起來,而不是僅僅在這里寫它們
uj5u.com熱心網友回復:
您不能Intent用于Fragment從導航到Activity。因此,您會收到此錯誤。作為,意圖接收 2 引數Intent(FromActivity, ToActivity.class) 。您在第二個引數中使用片段。我們知道,當我們創建一個活動時,它用AndroidManifest.xml檔案宣告。在這里,在您的清單檔案中沒有任何名為InformationFragment.
你可以學習 Android Navigation Component 來輕松地與 Activity 和 Fragment 進行通信。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/313492.html
標籤:爪哇 安卓 xml 安卓工作室 android-清单
