【问题标题】:Declare activity in AndroidManifest.xml to use an Intent在 AndroidManifest.xml 中声明活动以使用 Intent
【发布时间】:2020-04-26 18:47:02
【问题描述】:

我尝试将以下 Intent 对象从片段 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 中,我有以下代码来尝试取回该对象:

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 添加一个活动声明,但我不知道我应该添加什么/应该如何格式化。就像注释一样,我已经尝试四处寻找预先存在的问题,但我仍然不知道在我的清单中究竟要写什么。谢谢!

【问题讨论】:

    标签: java android android-studio android-layout android-xml


    【解决方案1】:

    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();
    

    然后您可以检索SixthFragmentonCreateView 中的参数值:

    public class SixthFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            OfficeHour add = (OfficeHour) getArguments().getSerializable("officeHour");
            // ...
        }
    }
    

    或者,您可以将SixthFragment 嵌入到它自己的Activity 中,并使用意图来启动它。

    您可以在官方Fragmentdocumentation找到更多信息。

    【讨论】:

    • 您能澄清一下您所说的 Activity 布局资源是什么意思吗?我在哪里可以找到那个?谢谢!
    • 在您的项目中,您应该找到一个带有layout 子文件夹的src/main/resresources 文件夹。应该有一个main_activity.xml 文件(或类似文件)声明相应Activity 的布局。在此文件中,您可以定义不同的组件,您可以为其分配资源 ID (android:id)。在创建了具有正确 id (android:id="@+id/myfragment") 的 <fragment> 组件后,您可以使用 R.id.myfragment 引用它。
    【解决方案2】:

    Intent 用于开始新的活动。您不能使用 Intent 更改片段。 更改片段。

    试试下面的链接和代码来实现片段。

    fragmnets 1Fragments in Detail

       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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-04
      • 2011-09-14
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多