简体   繁体   English

StartActivity上的Android.Content.ActivityNotFoundException

[英]Android.Content.ActivityNotFoundException at StartActivity

I'm currently developing an Androd app in Visual Studio 2017 using Xamarin and I'm somewhat of a beginner. 我目前正在使用Xamarin在Visual Studio 2017中开发Androd应用程序,我还是一个初学者。 My main activity is "MainActivity.cs", and in this activity I navigate to another activity on a button click. 我的主要活动是“ MainActivity.cs”,在此活动中,单击按钮即可导航到另一个活动。 I do this with the code: 我用代码来做到这一点:

var activity2 = new Intent(this, typeof(Menu2));
activity2.PutExtra("Token", Token);
StartActivity(activity2);

Everything works fine. 一切正常。 Then, in the second activity, "Menu2.cs", I try to navigate to another activity on a button click, but I get an error. 然后,在第二个活动“ Menu2.cs”中,我尝试单击按钮导航到另一个活动,但是出现错误。 I use the same code: 我使用相同的代码:

var activity3 = new Intent(this, typeof(Menu3));
StartActivity(activity3);

But I get the following error: 但是我收到以下错误:

Android.Content.ActivityNotFoundException: 
Unable to find explicit activity class {RWS.RWS/md563c841bc41fc48076b499c07864126c3.Menu3};
have you declared this activity in your AndroidManifest.xml?

Now, I tried adding the activities manully on the AndroidManifest, since Visual Studio does not add them automatically. 现在,我尝试在AndroidManifest上手动添加活动,因为Visual Studio不会自动添加活动。 This didn't work as well. 这也不起作用。 Currently, my AndroidManifest file is like this: 当前,我的AndroidManifest文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  package="RWS.RWS" android:versionCode="1" android:versionName="1.0" 
  android:installLocation="auto">
    <uses-sdk android:minSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="RWS" android:icon="@drawable/rws"></application>
</manifest>

Also, if it helps, the headers of my activities (Menu2 and Menu3) are the following: 另外,如果有帮助,我的活动(Menu2和Menu3)的标题如下:

[Activity(Label = "Menu2", MainLauncher = true, Icon = "@drawable/EAD", Theme = "@android:style/Theme.Light.NoTitleBar.Fullscreen")]
public class Menu2 : Activity

[Activity(Label = "Menu3", Icon = "@drawable/EAD", Theme = "@android:style/Theme.Light.NoTitleBar.Fullscreen")]
public class Menu3 : Activity

Menu2 and Menu3 full class as per asked by @Ferdous Ahamed: 按@Ferdous Ahamed的要求进行Menu2和Menu3的全班学习:

namespace RWS
{
    [Activity(Label = "Menu2", MainLauncher = true, Icon = "@drawable/EAD", Theme = "@android:style/Theme.Light.NoTitleBar.Fullscreen")]
    public class Menu2 : Activity
    {
   protected override void OnCreate(Bundle savedInstanceState)
   {
       base.OnCreate(savedInstanceState);
       string token = Intent.GetStringExtra("Token");

       // Create your application here
       SetContentView(Resource.Layout.Menu2);

       Button trabalho = FindViewById<Button>(Resource.Id.trabalho);
       trabalho.Click += delegate
       {
           Toast.MakeText(this, "Aguarde...", ToastLength.Short).Show();
       };

       Button pesquisa = FindViewById<Button>(Resource.Id.pesquisa);
       pesquisa.Click += delegate
       {
           var activity2 = new Intent(this, typeof(Menu3));
           //activity2.PutExtra("Token", Token);
           StartActivity(activity2);

       };

   }
   }
}

namespace RWS
{
     [Activity(Label = "Menu3")]
     public class Menu3 : Activity
     {

         protected override void OnCreate(Bundle savedInstanceState)
         {
             base.OnCreate(savedInstanceState);
             SetContentView(Resource.Layout.Menu3);
         }
     }
 }

Please note that Menu2 works properly while not having any difference and not being added on AndroidManifest, but Menu3 doesn't. 请注意,Menu2正常运行,没有任何区别,也未在AndroidManifest上添加,但Menu3没有。 The activities were added the same way, and one works, one doesn't. 这些活动是以相同的方式添加的,一种有效,一种无效。

What can I do? 我能做什么?

Use: 采用:

[Activity(Label = "Menu3")]
public class Menu3 : Activity

Instead of: 代替:

[Activity(Label = "Menu3", Icon = "@drawable/EAD", Theme = "@android:style/Theme.Light.NoTitleBar.Fullscreen")]
public class Menu3 : Activity

Finally, clean and rebuild your project. 最后, cleanrebuild您的项目。

It seems it wasn't actually this error. 看来这实际上不是此错误。 I tried rebuilding the project and looking at the output logs, but I never saw an actual code error, until I created a new project, rebuilt everything and then I was able to see an error in one of my .xaml file that was bugging the project. 我尝试重建项目并查看输出日志,但是直到创建新项目并重建所有内容之后,我再也没有看到实际的代码错误,然后我可以在自己的.xaml文件中看到一个错误,该错误已项目。

Quick tip: If Visual Studio is throwing an error, even if the error doesn't seem right, try to Clean the project and Rebuild. 快速提示:如果Visual Studio引发错误,即使该错误似乎不正确,也请尝试清理项目并重新生成。 After, check Output log and you'll find if your project has errors or not. 之后,检查输出日志,您将发现您的项目是否有错误。

Thanks to those who helped me. 感谢那些帮助我的人。

I had the same problem. 我有同样的问题。 It was about namespaces. 这是关于名称空间的。

I made a catalog inside my project "activities", made new activity file inside it and VS automaticly set namespace "myProject.activities" in this file. 我在项目“活动”中创建了目录,在其中创建了新的活动文件,并且VS在该文件中自动设置了命名空间“ myProject.activities”。 Then the problem occured. 然后出现了问题。

So I changed namespace in activity to "myProject" and it worked well. 因此,我将活动中的名称空间更改为“ myProject”,并且效果很好。

I did like this, it fixed my problem. 我确实这样,解决了我的问题。

[Activity(name = "RWS.RWS.Menu3")]
public class Menu3 : Activity
{
}

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

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