android.content.ActivityNotFoundException:无法找到显式活动类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10908534/
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: Unable to find explicit activity class
提问by Housefly
I get this error:
我收到此错误:
06-06 10:45:19.685: E/AndroidRuntime(554): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.Android.myApp/com.Android.myApp.Facebook.Example}; have you declared this activity in your AndroidManifest.xml?
But i have declared it in my manifest file. what might be the other reasons for such exception?
但是我已经在我的清单文件中声明了它。这种例外的其他原因可能是什么?
My manifest.xml
我的清单.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Android.myApp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light" >
<activity
android:name=".SignUpActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SignInActivity"
android:label="@string/app_name" />
<activity
android:name=".HomeActivity"
android:label="@string/app_name" />
<activity
android:name=".selectCityActivity"
android:label="@string/app_name" />
<activity
android:name=".FeedListViewActivity"
android:label="@string/app_name" />
<activity
android:name=".SearchActivity"
android:label="@string/app_name" />
<activity
android:name=".IWantActivity"
android:label="@string/app_name" />
<activity
android:name=".DateActivity"
android:label="@string/app_name" />
<activity
android:name=".ShareActivity"
android:label="@string/app_name" />
<activity
android:name=".ShareProductActivity"
android:label="@string/app_name" />
<activity
android:name=".SharePriceActivity"
android:label="@string/app_name" />
<activity
android:name=".ShareStoreActivity"
android:label="@string/app_name" />
<activity
android:name=".ProfileActivity"
android:label="@string/app_name" />
<activity
android:name=".ShowMapActivity"
android:label="@string/app_name" />
<activity
android:name=".ParticularEntryActivity"
android:label="@string/app_name" />
<activity
android:name=".MyLocationActivity"
android:label="@string/app_name" />
<activity
android:name=".MapMarkerActivity"
android:label="@string/app_name" />
<activity
android:name=".BarcodeActivity"
android:label="@string/app_name" />
<activity
android:name=".BarcodeResult"
android:label="@string/app_name" />
<activity
android:name=".FeedbackActivity"
android:label="@string/app_name" />
<activity
android:name=".SplashActivity"
android:label="@string/app_name" />
<activity
android:name=".Example"
android:label="@string/app_name" />
<uses-library android:name="com.google.android.maps" />
</application>
</manifest>
回答by Rasel
You declared package name in the manifest as com.Android.myApp
and Activity Name .Example
.So android will search it from com.Android.myApp.Example
.
But your activity is residing in "com.Android.myApp/com.Android.myApp.Facebook.Example
".So give the activity name as .Facebook.Example
or full path as given below
In the manifest
您在清单中将包名称声明为com.Android.myApp
Activity Name .Example
。因此 android 将从com.Android.myApp.Example
. 但是您的活动驻留在“ com.Android.myApp/com.Android.myApp.Facebook.Example
”中。.Facebook.Example
因此,请在清单中提供活动名称或完整路径,如下所示
<activity
android:name="com.Android.myApp.Facebook.Example">
</activity>
回答by Amit kumar
you can also use
你也可以使用
<activity
android:name=".Facebook.Example"
android:label="@string/app_name" />
回答by Gaddo
I got a variation to this problem. I was launching an activity called "Settings"and getting the same error and making all the suggested changes to the manifest were not fixing the problem.
我得到了这个问题的一个变体。我正在启动一个名为“设置”的活动,并收到相同的错误,并对清单进行所有建议的更改都没有解决问题。
Thing is, in the calling activity, I was also using / importing android.provider.Settings, so from what I can see when trying to launch the activity it was getting confused between the two. Thus I changed this in the code rather than the manifest to include the full path:
事情是,在调用活动中,我也在使用/导入 android.provider.Settings,所以从我在尝试启动活动时所看到的情况来看,这两者之间很混乱。因此,我在代码而不是清单中更改了它以包含完整路径:
Intent launchScr = new Intent(this, com.foo.bar.Settings.class);
And it worked. Of course, the other, and better, way to solve this particular issue would be to use better names for my activities.
它奏效了。当然,解决这个特定问题的另一种更好的方法是为我的活动使用更好的名称。
HTH anyone with this variant of the problem.
HTH 任何有此问题变体的人。
回答by Praveenkumar
From your exception which one is your class - Facebook
or Example
Because, you declared in package in manifest as com.Android.myApp
But, for facebook
activity you declared com.android.myApp.Facebook.Example
And,
从您的异常中哪个是您的类 -Facebook
或者Example
因为,您在清单中的包中声明为com.Android.myApp
But,对于facebook
您声明的活动,com.android.myApp.Facebook.Example
并且,
If example is your activity means, you should declared your activity as com.Android.myApp.Facebook.Example
Because, its from different package or your Activity name as Facebook.Example
So better you can declare your Activity like below -
如果示例是您的活动方式,您应该将您的活动声明为com.Android.myApp.Facebook.Example
因为,它来自不同的包或您的活动名称为Facebook.Example
所以更好地您可以像下面这样声明您的活动 -
<activity
android:name="com.Android.myApp.Facebook.Example"
android:label="@string/app_name" />
回答by ComeIn
Just make sure your Activity is decorated with this attribute:
只需确保您的 Activity 装饰有此属性:
[Activity(Label = "Your App Name", MainLauncher=true)]
Note: Only Set MainLauncher if needed.
注意:仅在需要时设置 MainLauncher。
回答by HZDI
Sometimes,It's due to existence of the same class name (second parameter of the Intent) in different packages.
有时,这是由于不同包中存在相同的类名(Intent 的第二个参数)。
I found also that this occurs when you call startService instead of calling startActivity and vice versa.
我还发现当您调用 startService 而不是调用 startActivity 时会发生这种情况,反之亦然。
回答by James Jordan Taylor
This can happen on large projects when the cached manifest gets out of sync. I was able to fix it in the Android Studio terminal with ./gradlew clean
and then in the Android Studio menu bar selecting File> Invalidate Caches / Restartfor good measure
当缓存的清单不同步时,这可能发生在大型项目上。我能够在 Android Studio 终端中修复它,./gradlew clean
然后在 Android Studio 菜单栏中选择File> Invalidate Caches / Restart以进行良好的测量
回答by Iefimenko Ievgwn
You need to use this code if you wish to change your class dynamically from string:
Intent intent = new Intent().setClassName(this.getPackageName(), "com.some.other.name");
or you can put result from array or any other instead of "com.some.other.name". Cheers!
如果您希望从字符串动态更改您的类,您需要使用此代码:
Intent intent = new Intent().setClassName(this.getPackageName(), "com.some.other.name");
或者您可以将结果来自数组或任何其他而不是“com.some.other.name”。干杯!
回答by AndroidGuy
This works if you have an Activity object (which you need to launch):
如果您有一个 Activity 对象(您需要启动它),这会起作用:
intent.setClassName(CallingActivity.this, activityToLaunch.getComponentName().getClassName());