2017-10-09 5 views
1

Mon application a minSdk = 21 et targetSdk = 26Android - Erreur lors de la tentative d'ajout application raccourcis

Je voulais mettre en place les raccourcis app

J'ai ajouté la balise <meta-data> à la première activité qui est lancé lorsque l'application démarre.

<activity android:name=".SignInActivity" 
      android:theme="@style/SubscriptionsTheme.NoActionBar"> 
      <intent-filter> 
       <category android:name="android.intent.category.LAUNCHER" /> 
       <action android:name="android.intent.action.MAIN"/> 
      </intent-filter> 
      <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/> 
     </activity> 

Ensuite, je crée le répertoire xml-v25 et dans ce fichier shortcuts.xml:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" > 
    <shortcut 
     android:shortcutId="test" 
     android:icon="@drawable/plus" 
     android:shortcutShortLabel="test" 
     android:shortcutLongLabel="long test" > 

     <intent 
      android:action="android.intent.action.VIEW" 
      android:targetPackage="com.xxxxx.xxxxx.xxxxx" 
      android:targetClass="com.xxxxx.xxxxx.xxxxxx.main" /> 

    </shortcut> 
</shortcuts> 

lorsque je tente de construire l'application que je reçois les erreurs suivantes:

Error:error: 'long test' is incompatible with attribute android:shortcutLongLabel (attr) reference. Error:error: 'test' is incompatible with attribute android:shortcutShortLabel (attr) reference. Error:'long test' is incompatible with attribute android:shortcutLongLabel (attr) reference.

+3

Vous avez probablement besoin d'utiliser une ressource de chaîne – Zoe

Répondre

2

Hope this helps .

strings.xml 

<string name="long_shortcut">long test</string> 
    <string name="short_shortcut">test</string> 

Utilisez cette référence de chaînes dans Manifest.xml

<shortcut 
     android:shortcutId="test" 
     android:icon="@drawable/plus" 
     android:shortcutShortLabel="@string/short_shortcut" 
     android:shortcutLongLabel="@string/long_shortcut" > 

En Java, vous pouvez également attribuer cela à l'aide setLongLabel (CharSequence longLabel) et setShortLabel(CharSequence) de ShortcutInfo.Builder.

+0

Merci pour la réponse. Cela a fonctionné. Même si je ne comprends pas pourquoi je ne devrais pas être en mesure de les coder en dur, au moins pour tester – Daniele

+3

Manifest préfère la référence de Project pour tout 'ID' (lorsque vous travaillez avec fb_id, fabric_id ou map_id) /' String' (lors de l'attribution d'une chaîne directement). –