Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:从preferences.xml启动活动_Android_Android Intent_Preferenceactivity - Fatal编程技术网

Android:从preferences.xml启动活动

Android:从preferences.xml启动活动,android,android-intent,preferenceactivity,Android,Android Intent,Preferenceactivity,我想用标记从default preferences.xml启动一个活动。这些活动都经过了很好的测试,问题不在于此。(我正在我的应用程序中扩展PreferenceActivity,因此preferences.xml是“附带的”) 请看一下代码,出了什么问题 preferences.xml: .... <PreferenceCategory android:title="@string/titleEtcSetup"> <PreferenceScreen

我想用标记从default preferences.xml启动一个活动。这些活动都经过了很好的测试,问题不在于此。(我正在我的应用程序中扩展PreferenceActivity,因此preferences.xml是“附带的”) 请看一下代码,出了什么问题

preferences.xml:

.... 
<PreferenceCategory 
    android:title="@string/titleEtcSetup">
    <PreferenceScreen
        android:key="renameCourses"
        android:title="@string/titleRenameCourses"
        android:summary="@string/textRenameDisplayedCoursesNames">
        <intent
             android:action="android.intent.action.VIEW"
             android:targetPackage="my.notifier.ui"
             android:targetClass="my.notifier.ui.EditCoursesNamesActivity" />         
    </PreferenceScreen>
.....
</PreferenceCategory>
..... 
。。。。
.....
..... 
manifest.xml:

....
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.notifier.ui"....
....
<activity android:name=".EditCoursesNamesActivity" android:label="@string/titleRenameCourses">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
 .....
。。。。
我相信
标签必须在
内部,而不是


.....

当我遇到这个问题时,是因为我为我的活动制作了一个子包。当我将它移动到根包中时,首选项屏幕可以启动它。

我遇到了同样的问题。我通过在我的AndroidManifest.xml中声明操作实现了这一点,如下所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.myapp" android:versionName="1.3" android:versionCode="4">

...

    <activity android:name=".activities.MyActivity" 
              android:label="@string/my_activity_title"
              android:theme="@android:style/Theme.Black.NoTitleBar">
        <intent-filter>
           <action android:name="com.example.myapp.activities.MyActivity" />
           <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
    </activity>

...
然后在我的首选项xml文件中:

<PreferenceCategory
        android:title="@string/my_activity_title">
    <PreferenceScreen
        android:title="@string/my_activity_title" 
        android:summary="@string/my_activity_title">
        <intent android:action="com.example.myapp.activities.MyActivity"/>
    </PreferenceScreen>
</PreferenceCategory>

我可以通过将意图过滤器中的类别从

android.intent.category.DEFAULT

android.intent.category.PREFERENCE


我想如果你想让你的行为更加具体,只需删除整个类别节点

<activity
  android:name=".ui.DatapackSelectionActivity"
  android:label="@string/app_name"
  android:screenOrientation="portrait"
  android:theme="@android:style/Theme.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
    </intent-filter>
</activity>

此解决方案向您展示了如何将活动关联到首选项标题中

首先,必须在清单中声明目标活动,如下所示:

<activity android:name=".ui.activities.MyActivity">
    <intent-filter>
        <action android:name=".ui.activities.MyActivity" />
        <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<header
    android:title="@string/my_activity_title"
    android:summary="@string/my_activity_summary">
    <intent android:action=".ui.activities.MyActivity"/>
</header>

注意这里活动和动作的android:name是相同的

现在,在preferences.xml文件中,您只需声明如下所示的头文件:

<activity android:name=".ui.activities.MyActivity">
    <intent-filter>
        <action android:name=".ui.activities.MyActivity" />
        <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<header
    android:title="@string/my_activity_title"
    android:summary="@string/my_activity_summary">
    <intent android:action=".ui.activities.MyActivity"/>
</header>


这就是一切。

小心!
targetPackage
的值应该是应用程序的包id,正如在
AndroidManifest.xml
文件(在Gradle构建文件中定义)的根元素中声明的那样。它不必与活动类的Java包相同(人们通常将它们放在
“ui”
的子包中)


因此,在您的特定情况下,我打赌
targetPackage
应该是
“my.notifier”
,而不是
“my.notifier.ui”
(我必须查看清单才能确定)。

无需添加IntentFilter。您可以通过完全限定名称引用活动:

<intent android:targetPackage="my.notifier.ui"
    android:targetClass="my.notifier.ui.EditCoursesNamesActivity"/>

我通过在清单中声明
解决了相同的问题,如下所示:

<activity
    android:name="PeriodosActivity"
    android:label="Periodos"
    android:screenOrientation="portrait">

    <intent-filter>
        <action android:name="Periodos" /> /*Same as in the preference's <intent> element's action attribute*/
        <category android:name="android.intent.category.DEFAULT"/>

</intent-filter>

/*与首选项元素的action属性中的相同*/

由于重构包名称,targetPackage和targetClass(前缀)可能不同。检查它的简单方法您可以删除清单中的活动并调用
startActivity()
,然后您将在logCat中看到此错误:“无法找到显式活动类{'targetPackage'/'targetClass'}”。这些是您应该写入首选项>意图的正确名称。不需要意向过滤器。

我也遇到了同样的问题。 并由此解决

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.coding1.myapp">
<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"
            > 
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
// ==================== HERE ================================
        <activity
            android:name="yaran.AccountWorker.AuthenticatorActivity"
            android:label="@string/app_name"
            >
            <intent-filter>
                <action android:name="AuthenticatorActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
// ========================================================
        <service android:name="yaran.AccountWorker.AuthenticatorService" android:exported="false"  android:process=":auth">
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>
            <meta-data android:name="android.accounts.AccountAuthenticator"
                       android:resource="@xml/authenticator" />
        </service>
    </application>

//=================================这里================================
// ========================================================
优先考虑:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="General Settings" />

    <Preference
        android:key="account_settings"
        android:title="Account Settings"
        android:summary="">
        <intent
            android:action="AuthenticatorActivity"
            android:targetPackage="com.example.coding1.myapp"
            android:targetClass="yaran.AccountWorker.AuthenticatorActivity" />
    </Preference>
</PreferenceScreen>



请小心在build.gradle(应用程序)中使用packgename
applicationId“com.some.some”
我的包在清单和类中都是这样的“com.some.love” 我在运行时遇到异常。这解决了我的问题

参考@style-7

谢谢你的帖子!但一切都是一样的。(顺便说一句:我在base使用内置API演示来创建代码,它在PreferenceScreen中包含意图标记)我在我的三星Galaxy S上尝试过这段代码,效果很好。你在哪个Android版本上试过这个?我在AVD上试过,所有的7个API bethween版本都是4-10。我不知道有什么问题…:(…我想问你:请用
标签试一下。也许它也可以在你的设备上运行…是的,它确实运行了。我更改了名称并用字符串替换了字符串引用,但我认为这些更改并不能使它运行。据我所知,任何其他响应action
activities.MyActivity
的应用程序也有资格进入该字段开始的意图。这有点不受欢迎,特别是如果你有免费/专业版的应用程序。谢谢,你是对的。你不需要任何
。在gradle!@MortenHolmgaard!上使用不同的构建类型/产品风格时,请记住这一点!但是你如何引用该软件包呢(例如:
buildTypes.debug.applicationIdSuffix=“.debug”
)@TWiStErRob你应该参考你的basepackage-据我所知,如果你有一个特殊的buildtype文件,你会得到你的buildtype特定的文件,这取决于你的Bbuild变体。我使用的是产品风格。android:targetPackage应该是应用程序Id而不是包Id。我只是想我们应该清楚术语,这样我们就不会混淆你。@BoD,请更新这个答案,指出现在重要的包在你的gradle中,而不是在你的清单中。我只是有同样的问题,但我找到了一个不同的解决方案。假设我的
活动在
com.example.subpackage.Activity
…那么
意图
元素应该看起来像
@Tim Stack我不同意。你不能不同意,这只是事实。只有代码的答案不被认为是一个好答案,会被审查系统挑选出来进行质量审查
<Preference>
    <intent
        android:targetPackage="applicationIdFromBuildGradle"
        android:targetClass="targetClassFromJavaFile.YourClassName"/>
</Preference>
<Preference>
<intent
    android:targetPackage="applicationIdFromBuildGradle"
    android:targetClass="targetClassFromJavaFile.YourClassName"/>