gpt4 book ai didi

安卓.content.ActivityNotFoundException : Unable to find explicit activity class - Intent in a Fragment

转载 作者:行者123 更新时间:2023-11-30 01:34:09 45 4
gpt4 key购买 nike

当我使用该应用程序时,该应用程序导致运行时出错,这是一个 Intent 错误,但据我所见,代码中没有错误,它说:

    android.content.ActivityNotFoundException: Unable to find explicit activity class .... have you declared this activity in your AndroidManifest.xml?

但是它不需要在 list 中说明,因为它是一个 fragment ,我已经在 list 中说明了它从中膨胀的 Activity 。

        @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //I
Intent i = new Intent(ViewAllRecipe.this.getActivity(), EditRecipesFragment.class);
HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
String RecId = map.get(Config.TAG_ID).toString();
i.putExtra(Config.REC_ID, RecId);
getActivity().startActivity(i);
}

错误来自:

    getActivity().startActivity(i);

它被膨胀的 Activity 是,其他 fragment 也只有这个工作正常;

       <activity
android:name=".NavigationActivity"
android:label="@string/app_name" >
</activity>

谢谢!

最佳答案

您可以使用此方法从不同的 Activity/Fragment 调用一个 Activity

Intent intent = new Intent(context, YourNewActivity.class);

上面的构造函数将占用下面解释的两个参数。

1).一个 Context 作为它的第一个参数(使用这个是因为 Activity 类是 Context 的子类)

2).系统应向其传递 Intent 的应用程序组件的 Class(在本例中,应启动的 Activity)

但是在你的例子中,第二个参数是一个 Fragment,它不依赖于上面的语法,你得到 ActivityNotFoundException 的原因是因为在这一行

Intent i = new Intent(ViewAllRecipe.this.getActivity(), EditRecipesFragment.class);

JVM 期望第二个参数是一个 Activity 类,当它在 manifest 中找不到任何这样的类时,它会抛出 ActivityNotFoundException.

要从 Activity 调用 Fragment,您必须使用 FragmentTransactions

关于安卓.content.ActivityNotFoundException : Unable to find explicit activity class - Intent in a Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35325842/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com