android.content.ActivityNotFoundException:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3433778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android.content.ActivityNotFoundException:
提问by sharath
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.x.y/com.x.y.class};
android.content.ActivityNotFoundException:无法找到显式活动类 {com.xy/com.xyclass};
I am doing an intent.setClass("com.x.y","com.x.y.className")
where className
is the name of my activity class and com.x.y
is the package it resides in.
我正在做一个intent.setClass("com.x.y","com.x.y.className")
whereclassName
是我的活动类的名称,com.x.y
是它所在的包。
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?
我错过了什么吗?
回答by Mina Samy
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>
回答by thepearson
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
. (my issue was caused by Typeface.createFromAsset()
)
我得到的这个异常实际上并不是由意图引起的,而是我在加载的活动中运行的一些代码抛出一个RuntimeException
. (我的问题是由 引起的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. Like so:
要查看是否是这种情况,请将您的意图代码放在 try catch 块中。像这样:
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.
我花了一个小时试图弄清楚这一点。希望这可以节省一些时间。
回答by Gangnus
The activity you are calling should appear not only in the Manifest for its own package, but in the Manifest for the CALLING package, too.
您正在调用的活动不仅应该出现在它自己的包的清单中,而且还应该出现在 CALLING 包的清单中。
回答by bicha
Delete your activity from the manifest and then add it again. This type do not write type the XML directly. Instead, go to Application > Application nodes > add
, choose the Activity, and then browse for the file source.
从清单中删除您的活动,然后再次添加。这种类型不直接写类型XML。相反,转到Application > Application nodes > add
,选择活动,然后浏览文件源。
This worked for me.
这对我有用。
回答by Andy Lin
intent.setClass takes parameters as "Package Context" and "Class". an example would be:
intent.setClass 将参数作为“Package Context”和“Class”。一个例子是:
intent.setClass(CurrentActivity.this, TargetActivity.class);
also you need to check if the activity is registered in manifest file.
您还需要检查活动是否已在清单文件中注册。
回答by M.K.
Added a new activity and defined it in manifest.xml, but I was still getting "Unable to find explicit activity class" error. I am using 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.
添加了一个新活动并在 manifest.xml 中定义它,但我仍然收到“无法找到明确的活动类”错误。我正在使用 Eclipse。我的问题的解决方案是“清理”项目。从 Eclipse 的主菜单中:Project|Clean...。然后选择您的项目并清理它。
回答by Jalp
Hey, you need to use another form of Intent constructor. This will surely solve your issue within a second:
嘿,您需要使用另一种形式的 Intent 构造函数。这肯定会在一秒钟内解决您的问题:
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.
这工作得很好,我检查了这段代码,它工作正常。
回答by Carl K.
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)并相应地更改清单后,它照常工作。
回答by meboy
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. Would like to hear what it would be.
我有一种感觉,一个更聪明的 Android 人可以解决这个问题,而不必这样做。想听听会是什么。
回答by mabeiyi
Yeah I got this problem too. I refreshed the project. And then, everything works fine.
是的,我也遇到了这个问题。我刷新了项目。然后,一切正常。