xamarin Android:Java.Lang.RuntimeException with intent

pgx2nnw8  于 2023-08-01  发布在  Android
关注(0)|答案(1)|浏览(149)

我在Android上开发了两个应用程序:应用程序A,当按下按钮时,应用程序A应打开应用程序B,应用程序B应向应用程序A返回值,如“OK”。为此,应用A使用操作“com.intent.test”发送Intent,但当它接收到Intent时,应用B崩溃并显示以下错误消息:
Java.Lang.RuntimeException:'无法示例化活动ComponentInfo{com.example.newdemo/com.example.newdemo.MainActivity}:java.lang.ClassNotFoundException:在以下路径中未找到类“com.example.demointent.MainActivity”:DexPathList[[zip file“/data/app/com.example. demoint-SsbNhZc7TrVyMjaIbZDfVw ==/base.apk”],nativeLibraryDirectories=[/data/app/com.example. demoint-SsbNhZc7TrVyMjaIbZDfVw ==/lib/arm64,/data/app/com.example. demoint-SsbNhZc7TrVyMjaIbZDfVw ==/base.apk!/lib/arm64-v8a,/system/lib64,/system/product/lib64]]'
AndroidManifest.xml应用程序B:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.example.demointent" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" />
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" android:label="@string/app_name" android:exported="true" android:enabled="true">
            <intent-filter>
                <action android:name="com.intent.test" />
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

字符串
MainActivity应用程序B:

namespace NewDemo
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            try
            {
                base.OnCreate(savedInstanceState);
                Xamarin.Essentials.Platform.Init(this, savedInstanceState);
                SetContentView(Resource.Layout.activity_main);

                Intent receivedIntent = Intent;
                if (receivedIntent == null) return;

                var dataReceived = receivedIntent.GetStringExtra("key");
                var action = receivedIntent.Action;

                var intent = new Intent();
                switch (action)
                {
                    case "com.intent.test":
                        intent.PutExtra("result", "OK");
                        break;
                }

                SetResult(Result.Ok, intent);
                Toast.MakeText(this, "My app is started. Data: " + dataReceived, ToastLength.Short)?.Show();
                Finish();
            }
            catch (Exception ex)
            {
                Toast.MakeText(this, "error: " + ex.Message, ToastLength.Short)?.Show();
            }

        }
    }
}


MainActivity应用程序A:

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);
            var btnstartWithAction = FindViewById<TextView>(Resource.Id.btnstartWithAction);
            if (btnstartWithAction != null)
            {
                btnstartWithAction.Click += (sender, e) =>
                {
                    var intent = new Intent();
                    var action = "com.intent.test";
                    intent.SetAction(action);
                    intent.PutExtra("key", "keytest");

                    StartActivityForResult(Intent.CreateChooser(intent, "App"), 3);
                };
            }
        }


当我将Intent与操作一起使用时,会出现此错误。当应用程序A用包名调用应用程序B时,我没有问题。像这样:

Intent launchIntent = ApplicationContext.PackageManager.GetLaunchIntentForPackage("com.example.newdemo");
                    launchIntent?.SetFlags(0);
                    launchIntent?.SetAction("com.intent.test");
                    launchIntent?.PutExtra("key", "12345");
                    StartActivityForResult(Intent.CreateChooser(launchIntent, ""), 12);


我已经清理和建立我的项目几次。我使用Xamarin和Visual Studio 2022编写C#代码。有人知道发生了什么吗- 谢谢-谢谢

k3fezbri

k3fezbri1#

Java.Lang.RuntimeException:'无法示例化活动ComponentInfo{com.example.newdemo/com.example.newdemo.MainActivity}:java.lang.ClassNotFoundException:未找到类“com.example.demointent.MainActivity”
Xamarin.Android上,默认情况下,使用基于MD5的类名生成Java Package 器,以避免Java类名冲突,例如:

md579731053346ff64fcf21847b09163ce1.MainActivity

字符串
您在清单中硬编码了一个activity android:name=".MainActivity",但生成的类默认情况下将基于MD5。
您应该避免混淆声明性属性和手动编写AndroidManifest.xml
[Activity (Label = "MyApp", MainLauncher = true, Icon = "@mipmap/ic_launcher")]在实际使用(生成)的AndroidManifest.xml中生成一段代码,看起来像:

<activity android:icon="@drawable/icon" android:label="AndroidApp1" android:name="md5c178831cd46fc53bebc42cf953f78ced.MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>


你的代码会在别的地方您可以在输出文件夹.\obj\Debug\android中找到生成的AndroidManifest.xml
因此,尝试从清单文件中删除手动编辑,并在需要时通过属性添加内容。
举例来说:

[Activity(Label = "SearchBarDemos", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true,Exported =true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
    [
    IntentFilter
    (
        new[] { Android.Content.Intent.ActionView },
        Categories = new[]
            {
                Android.Content.Intent.CategoryDefault,
                Android.Content.Intent.CategoryBrowsable
            },
        DataSchemes = new[] { "myapp" }
    )
]
    public class MainActivity:Activity
 {

 }

相关问题