Exception when launching activity from PreferenceScreen

917 views Asked by At

I'd like to launch an activity from a preference screen. This already did work in the past and I haven't changed any code but updated the Android SDK (updated packages and installed new ones) and since then it doesn't work anymore. I get an exception that the activity could not be found.

What am I doing wrong, what am I missing?

My activity:

namespace com.mycompany.myproject.config
{
    [Activity(
        Label = "@string/config_bt_select_device", 
        ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.Orientation
    )]
    public class actDeviceList : Activity

My preference screen XML:

<PreferenceScreen
  android:key="DeviceAddress"
  android:title="@string/config_optDeviceAddress">

  <intent android:action="android.intent.action.VIEW"
    android:targetPackage="com.mycompany.myproject"
    android:targetClass="com.mycompany.myproject.config.actDeviceList"
    android:data="RadioReceiverDevice"
  />
</PreferenceScreen>

My manifest.xml (in Xamarin):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.mycompany.myproject" android:versionName="1.3.10" android:versionCode="48">
    <uses-sdk android:targetSdkVersion="21" android:minSdkVersion="12" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:icon="@drawable/general_action_about" android:label="My Project Name" android:theme="@style/ProjectTheme" />
</manifest>

Exception I get when I click on the preference item:

06-15 16:31:47.780 I/InputDispatcher(  898): Delivering touch to (14502): action: 0x1, toolType: 1
06-15 16:31:47.780 I/Timeline(14502): Timeline: Activity_launch_request id:com.mycompany.myproject time:1665066
06-15 16:31:47.780 E/PersonaManagerService(  898): inState():  stateMachine is null !!
06-15 16:31:47.780 I/ActivityManager(  898): START u0 {act=android.intent.action.VIEW dat=Device cmp=com.mycompany.myproject/.config.actDeviceList} from uid 11098 on display 0
06-15 16:31:47.780 E/InputEventReceiver(14502): Exception dispatching input event.
06-15 16:31:47.790 E/MessageQueue-JNI(14502): Exception in MessageQueue callback: handleReceiveCallback
06-15 16:31:47.790 E/MessageQueue-JNI(14502): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.mycompany.myproject/com.mycompany.myproject.config.actDeviceList}; have you declared this activity in your AndroidManifest.xml?
06-15 16:31:47.790 E/MessageQueue-JNI(14502):   at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
06-15 16:31:47.790 E/MessageQueue-JNI(14502):   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1499)
06-15 16:31:47.790 E/MessageQueue-JNI(14502):   at android.app.Activity.startActivityForResult(Activity.java:3913)
06-15 16:31:47.790 E/MessageQueue-JNI(14502):   at android.app.Activity.startActivityForResult(Activity.java:3860)
06-15 16:31:47.790 E/MessageQueue-JNI(14502):   at android.app.Activity.startActivity(Activity.java:4184)

Edit:

AndroidManifest.xml created by Xamarin during the build process:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.mycompany.myproject" android:versionName="1.3.10" android:versionCode="48">
      [... permissions ...]
      <application android:icon="@drawable/general_action_about" android:label="My Project Name" android:theme="@style/ProjectTheme" android:name="md5ab60cc6a69ae4b801c5b8b198bf1c300.ProjectApp" android:debuggable="true">
        [...lots of activities...]
        <activity android:configChanges="keyboardHidden|orientation" android:label="@string/config_bt_select_device" android:name="md50664b305cd453fdd36618c371adf64ff.actDeviceList" />
      </application>
    </manifest>
2

There are 2 answers

2
Cheesebaron On BEST ANSWER

The issue here is that you need to use the [Register] attribute on your Activity. This is required since Xamarin.Android 5.1 as it will otherwise prepend your activities with a MD5 sum, such that the package name is unique.

So do something like this:

[Register("com.mycompany.myproject.config.actDeviceList")]
[Activity(Label = "Device List" ...)]
public class actDeviceList : Activity
{
    ...
}

Otherwise you can also explicitly specify a Activity name in the Activity attribute, using the Name property.

You would have found this out if you had read the release notes.

0
siva On

I think you should specify the relative path for target class in your preference XML file rather than full class path.