应用程序的默认包是example.app".
The application's default package is "example.app".
目标活动的包是example.app.abc".
and the target activity's package is "example.app.abc".
在 java 代码中为example.app.abc.TheActivity"调用 startActivity() 可以正常工作,
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>
我尝试了 android:targetClass="example.app.abc.TheActivity" 但它也不起作用.
I tried android:targetClass="example.app.abc.TheActivity" but it doesn't work too.
是否可以优先启动非默认包的活动?
Is it impossible to start an activity of non-default package, in preference?
我在尝试使用 custom 时遇到了同样的问题来自 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 {
}
在你的 AndroidManifest.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" />
顺便说一句,语法非常繁琐,至少对于帐户偏好而言.所有这些变体都失败了:
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"/>
关键因素显然是 android: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" />
这篇关于PreferenceScreen 中不同包的 targetClass 时的 ActivityNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!