[英]Error: have you declared this activity in your AndroidManifest.xml? when trying to intent a class
[英]Declare activity in AndroidManifest.xml to use an Intent
我有以下意圖 object 我試圖從片段 SeventhFragment.java 傳達給另一個片段(SixthFragment.java)。
Intent i = new Intent(getContext(), SixthFragment.class);
i.putExtra("officeHour", hour);
startActivity(i); // this code is in SeventhFragment.java
在 SixthFragment.java 中,我有以下代碼試圖讓這個 object 回來:
Intent intent = (Intent) getActivity().getIntent(); // this code is in SixthFragment.java.
OfficeHour add = (OfficeHour) intent.getSerializableExtra("officeHour");
但是,我遇到了一個例外:
android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.example.app/com.example.app.SixthFragment}; have you declared
this activity in your AndroidManifest.xml?
我可以說這意味着我需要向 AndroidManifest.xml 添加一個活動聲明,但我不知道我應該添加什么/應該如何格式化。 就像一個注釋一樣,我已經嘗試四處尋找預先存在的問題,但我仍然不知道在我的清單中究竟要寫什么。 謝謝!
Fragment
始終需要由Activity
托管。 您不能使用Intent
在片段之間轉換。 相反,您可以使用FragmentTransaction
並將數據作為參數傳遞:
// this code is in SeventhFragment's underlying Activity
Fragment f = new SixthFragment();
Bundle args = new Bundle();
args.putSerializable("officeHour", hour);
f.setArguments(args);
// Execute a transaction to replace SeventhFragment with SixthFragment
FragmentTransaction ft = getFragmentManager().beginTransaction();
// R.id.myfragment needs to be defined in your Activity's layout resource
ft.replace(R.id.myfragment, f);
ft.commit();
然后,您可以在SixthFragment
的onCreateView
中檢索參數值:
public class SixthFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
OfficeHour add = (OfficeHour) getArguments().getSerializable("officeHour");
// ...
}
}
或者,您可以將SixthFragment
嵌入到它自己的Activity
中並使用意圖來啟動它。
您可以在官方Fragment
文檔中找到更多信息。
Intent 用於開始新的活動。 您不能使用 Intent 更改片段。 更改片段。
試試下面的鏈接和代碼來實現片段。
FristFragment firstFragmentInstance=new FirstFragment();
FragmentManager firstFragmentManager=getSupportFragmentManager();
FragmentTransaction firstFragmentTransaction=firstFragmentManager.beginTransaction();
firstFragmentTransaction.add(R.id.content_main,firstFragmentInstance,"").commit();
並替換碎片
FristFragment firstFragmentInstance=new FirstFragment();
FragmentManager firstFragmentManager=getSupportFragmentManager();
FragmentTransaction firstFragmentTransaction=firstFragmentManager.beginTransaction();
firstFragmentTransaction.replace(R.id.content_main,firstFragmentInstance,"first_fragment_tag").addToBackStack(null).commit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.