简体   繁体   English

android.content.ActivityNotFoundException:

[英]android.content.ActivityNotFoundException:

I am getting this exception while I am trying to call an activity from another one.我在尝试从另一个活动调用活动时遇到此异常。 The complete exception is完整的例外是

android.content.ActivityNotFoundException:Unable to find explicit activity class {com.xy/com.xyclass}; android.content.ActivityNotFoundException:无法找到显式活动类 {com.xy/com.xyclass};

I am doing an intent.setClass("com.xy","com.xyclassName") where className is the name of my activity class and com.xy is the package it resides in.我正在做一个intent.setClass("com.xy","com.xyclassName")其中className是我的活动类的名称, com.xy是它所在的包。

My AndroidManifest.xml has the following content:我的 AndroidManifest.xml 有以下内容:

<activity android:name="com.x.y.className" android:label="@string/app_name">

Am I missing anything?我错过了什么吗?

Maybe you need to check that you added the new activity to the manifest.xml file也许您需要检查您是否将新活动添加到 manifest.xml 文件中

Example:例子:

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

If other people are encountering something similar and arrive at this post, an issue I had may save you some time.如果其他人遇到类似问题并到达此帖子,我遇到的问题可能会为您节省一些时间。 May not be related to the OP's problem but def related to the ActivityNotFound exception.可能与 OP 的问题无关,但与 ActivityNotFound 异常有关。

I was trying to load an activity by using:我试图使用以下方法加载活动:

Intent intent = new Intent( this, class );

However I continuously kept getting the ActivityNotFoundException even though I had checked and rechecked the code multiple times.但是,即使我多次检查并重新检查代码,我仍然不断收到ActivityNotFoundException

This exception I was getting wasn't actually being caused by the intent but some code I was running inside the loaded activity throwing a RuntimeException .我得到的这个异常实际上并不是由意图引起的,而是我在加载的活动中运行的一些代码引发了RuntimeException (my issue was caused by Typeface.createFromAsset() ) (我的问题是由Typeface.createFromAsset()引起的)

It is possible you are running into a similar RuntimeException in your activity.您可能在活动中遇到类似的 RuntimeException。

To see if this is the case, put your intent code in try catch blocks.要查看是否是这种情况,请将您的意图代码放在 try catch 块中。 Like so:像这样:

try {
    /* your code */
    ...
} catch ( ActivityNotFoundException e) {
    e.printStackTrace();
}

Run your app again, and check your LogCat, if it's the same issue, you'll get a RuntimeException with a "Caused By:" entry pointing to your actual issue.再次运行您的应用程序,并检查您的 LogCat,如果它是相同的问题,您将收到一个 RuntimeException,其中包含一个指向您的实际问题的“Caused By:”条目。

I spent a good hour trying to figure this out.我花了一个小时试图弄清楚这一点。 Hopefully this may save someone some time.希望这可以节省一些时间。

您正在调用的活动不仅应该出现在它自己的包的清单中,而且还应该出现在 CALLING 包的清单中。

Delete your activity from the manifest and then add it again.从清单中删除您的活动,然后再次添加。 This type do not write type the XML directly.这种类型不直接写类型XML。 Instead, go to Application > Application nodes > add , choose the Activity, and then browse for the file source.相反,转到Application > Application nodes > add ,选择 Activity,然后浏览文件源。

This worked for me.这对我有用。

intent.setClass takes parameters as "Package Context" and "Class". intent.setClass 将参数作为“Package Context”和“Class”。 an example would be:一个例子是:

intent.setClass(CurrentActivity.this, TargetActivity.class);

also you need to check if the activity is registered in manifest file.您还需要检查活动是否已在清单文件中注册。

Added a new activity and defined it in manifest.xml, but I was still getting "Unable to find explicit activity class" error.添加了一个新活动并在 manifest.xml 中定义它,但我仍然收到“无法找到明确的活动类”错误。 I am using Eclipse.我正在使用 Eclipse。 Solution for my problem was "cleaning" the project.我的问题的解决方案是“清理”项目。 From the main menu in Eclipse: Project|Clean.... Then you select your project and clean it.从 Eclipse 的主菜单中:Project|Clean...。然后选择您的项目并清理它。

I was using getActivityContext() (instead of Activity.this ) for the menu code to save some work, and copy-and-paste it to each activity without editing each time.我使用getActivityContext() (而不是Activity.this )作为菜单代码来保存一些工作,并将其复制并粘贴到每个活动中,而无需每次都进行编辑。

I replaced them with Activity.this , and the issue is gone.我用Activity.this替换了它们,问题就消失了。

I have a feeling a smarter Android guy could work-around not having to do that.我有一种感觉,一个更聪明的 Android 人可以解决这个问题,而不必这样做。 Would like to hear what it would be.想听听会是什么。

Hey, you need to use another form of Intent constructor.嘿,您需要使用另一种形式的 Intent 构造函数。 This will surely solve your issue within a second:这肯定会在一秒钟内解决您的问题:

Example:例子:

Intent inte=new Intent(getBaseContext(),"your class name with .class extension ");

startActivity(inte);

This works perfectly and I checked this code, its working properly.这工作得很好,我检查了这段代码,它工作正常。

I had an ActivityNotFoundException when I implemented the Activity inside another class (as an inner class):当我在另一个类(作为内部类)中实现 Activity 时,我有一个 ActivityNotFoundException:

//... inside the utility class Pref
public static class Activity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
    }
}
//...

Declared as the following inside the manifest:在清单中声明如下:

<activity android:name=".Pref.Activity"
...

After declaring this as a normal class (public class PrefActicity) and changing manifest accordingly, it worked as usual.在将其声明为普通类(公共类 PrefActicity)并相应地更改清单后,它照常工作。

Yeah I got this problem too.是的,我也遇到这个问题。 I refreshed the project.我刷新了项目。 And then, everything works fine.然后,一切正常。

when i have same issue.当我有同样的问题时。 if you are using library class files and writing it into android manifest files write it like and then remove the library projects manifest files this portion>> then it will work absolutely..如果您正在使用库类文件并将其写入 android 清单文件,请像这样写然后删除库项目清单文件这一部分>>那么它绝对可以工作..

This exception also occurs if you include a library in your app and if the library is calling an activity defined in the library project.如果您在应用程序中包含一个库并且该库正在调用库项目中定义的活动,也会发生此异常。 In this case we need to merge library's manifest with calling app's manifest.在这种情况下,我们需要将库的清单与调用应用程序的清单合并。

With ADT version 20, we can do this by adding the below statement in project.properties of calling app.使用 ADT 版本 20,我们可以通过在调用 app 的 project.properties 中添加以下语句来实现。

manifestmerger.enabled=true manifestmerger.enabled=true

Check out the content of the Android Manifest File in the bin folder of the project.查看项目bin文件夹中Android Manifest File的内容。 When your app is compiled and packaged the Manifest File is copied to the bin folder.当您的应用程序被编译和打包时,清单文件被复制到 bin 文件夹中。 In my case the Manifest in the bin folder did not agree with the original Manifest.就我而言,bin 文件夹中的 Manifest 与原始 Manifest 不一致。 This is probably a mistake of Eclipse.这可能是 Eclipse 的错误。 I manually copied the Manifest to the bin folder and it worked.我手动将 Manifest 复制到 bin 文件夹并且它起作用了。

you can add this code in manifiest.xml file您可以在manifiest.xml文件中添加此代码

action android:name="com.kaushalam.activity101activity.SecondActivity"
category android:name="android.intent.category.DEFAULT"

I got the same case too.我也遇到了同样的情况。 After reading thepearson's answer, I revised my Activity and found out that I wrote看完thepearson的回答,我修改了我的Activity,发现我写的

public void onCreate(Bundle s)

But in fact it should be但实际上应该是

protected void onCreate(Bundle s)

And it works now!它现在有效!

如果您有一个 Activity 对象(您需要启动它),这会起作用:

intent.setClassName(CallingActivity.this, activityToLaunch.getComponentName().getClassName());

查看此处的文档,您想要的是:

intent.setClassName("com.x.y", "className");

Activity you're calling sholdn't contain "sheme" and contain intent-filter:您正在调用的活动不包含“sheme”并且包含意图过滤器:

<activity android:name=".SecondActivity">
        <intent-filter>
               <action android:name="com.example.sj.myapplication.SecondActivity"/>
               <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
</activity>

so in calling code:所以在调用代码中:

Intent intent=new Intent("com.example.sj.myapplication.SecondActivity");
startActivity(intent);

I also ran into ActivityNotFoundException by passing the wrong view into setContentView(), each activity's class file must correspond with the layout xml file this way.我还通过将错误的视图传递给 setContentView() 遇到了 ActivityNotFoundException,每个活动的类文件必须以这种方式与布局 xml 文件对应。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wrongView);
}

as opposed to

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.correctView);
}

Restart the Eclipse and check your Manifestfile again.重新启动 Eclipse 并再次检查您的 Manifestfile。 If you find missing the respective Activity, then add it and try again.如果您发现缺少相应的 Activity,请添加它并重试。 It solved my similar issue.它解决了我的类似问题。

Try using the following:尝试使用以下方法:

intent.setClassName("com.x.y", "com.x.y.className");

This works for me这对我有用

In addition to Mina's answer.除了米娜的回答。 When you declare activity as inner static class then you should write your activity into manifest like ...当您将活动声明为内部静态类时,您应该将您的活动写入清单,例如...

         <activity android:name=".app.FragmentLayoutSupport$DetailsActivity" />

here .app comes from your package name , it can be .helpers.afdfa$afda这里 .app 来自你的包名,它可以是 .helpers.afdfa$afda

My solution to this error was to add a package name in front of the name in manifest.我对此错误的解决方案是在清单中的名称前添加一个包名称。

I had the following activities:我进行了以下活动:

  • id.scanner.main.A1 id.scanner.main.A1

  • id.scanner.main.gallery.A2 id.scanner.main.gallery.A2

My manifest contained the following:我的清单包含以下内容:

<activity android:name=".A1" ....></activity>
<activity android:name=".A2" ....></activity>

This solved the problem:这解决了这个问题:

<activity android:name=".A1" ....></activity>
<activity android:name="gallery.A2" ....></activity>

I had the same issue.我遇到过同样的问题。 I tried everything but the error, which I sorted out later, was that there was a space left between double quotes and my class name.我尝试了所有方法,但后来解决的错误是双引号和我的类名之间留有空格。 It has to be: intent.setClassName("com.xy","com.xyclassName")它必须是: intent.setClassName("com.xy","com.xyclassName")

not不是

intent.setClassName("com.x.y","  com.x.y.className")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM