ActivityNotFoundException当不同的包的targetClass在preferenceScreen不同、ActivityNotFoundException、preferenceSc

2023-09-12 10:16:43 作者:我想我疯了

应用程序的默认套餐为example.app。

The application's default package is "example.app".

和目标活动的包是example.app.abc。

and the target activity's package is "example.app.abc".

在java中code调用startActivity()的example.app.abc.TheActivity只是工作,

Calling startActivity() for "example.app.abc.TheActivity" in java code just works,

但是,preference.xml指定这是行不通的:

but specifying it on preference.xml doesn't work:

<PreferenceScreen android:key="key"
    android:title="@string/title"
>
    <intent android:action="android.intent.action.MAIN"
        android:targetPackage="example.app.abc"
        android:targetClass="TheActivity"
/>
</PreferenceScreen>

我试过安卓targetClass =example.app.abc.TheActivity,但它不工作了。

I tried android:targetClass="example.app.abc.TheActivity" but it doesn't work too.

时无法启动非默认的包的活动,在preference?

Is it impossible to start an activity of non-default package, in preference?

推荐答案

我只是想使用定制的时候遇到了同样的问题, preference屏幕从库工程,为的AccountManager帐户设置。无论我怎样努力来调整targetPackage和targetClass属性,它会抛出一个异常(除外,因为它是一个帐户,它崩溃的电话)。

I just ran into the same problem when trying to use a custom preference screen from a library project for the AccountManager account settings. No matter how I tried to tweak the targetPackage and targetClass attributes, it would throw an exception (except, since it's an account, it crashes the phone).

我想我们只好认为这是一个Android的限制。这是笨拙的,但是你真正需要做的是应用程序的名称空间内声明的包装类活动:

I think we'll just have to assume this is an Android limitation. It's clumsy, but all you really need to do is declare a wrapper class for the activity within your application's namespace:

public class MyPreferences extends ActualPreferences {
}

声明它在你的Andr​​oidManifest.xml

Declare it in your AndroidManifest.xml

<activity android:name=".MyPreferences"/>

然后你就可以在你的意图指定类

Then you can specify the class in your intent

<intent android:targetPackage="com.my.package"
        android:targetClass="com.my.package.MyPreferences" />

顺便说一句,的语法是非常挑剔,至少帐户preferences。所有这些变化失败:

By the way, the syntax is extremely fussy, at least for account preferences. All these variations fail:

<!-- fails --> <intent android:targetClass="com.my.package.MyPreferences" />
<!-- fails --> <intent android:targetClass="MyPreferences" 
                       android:targetPackage="com.my.package"/>
<!-- fails --> <intent android:targetClass=".MyPreferences"
                       android:targetPackage="com.my.package"/>
<!-- fails --> <intent android:targetClass="settings.MyPreferences"
                       android:targetPackage="com.my.package"/>
<!-- fails --> <intent android:targetClass=".settings.MyPreferences"
                       android:targetPackage="com.my.package"/>
<!-- fails --> <intent android:targetClass="com.my.other.package.MyPreferences"
                       android:targetPackage="com.my.package"/>

的关键因素是明显的安卓targetPackage 属性的应用程序包相匹配。如果你愿意,你可以把一个子包的活动。这工作:

The critical factor is apparently that the android:targetPackage attribute matches the application package. If you want, you can put the activity in a sub-package. This works:

<intent android:targetPackage="com.my.package"
        android:targetClass="com.my.package.settings.MyPreferences" />
 
精彩推荐
图片推荐