2017-10-09 31 views
1

我的應用程序有minSdk = 21和targetSdk = 26Android的 - 當添加應用程序錯誤快捷鍵

我想建立應用程序的快捷方式

我已經添加了<meta-data>標籤這是第一個活動當應用程序啓動時啓動。

<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> 

然後我創建了xml-v25目錄並從中這個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> 

當我嘗試構建應用程序,我得到了以下錯誤:

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

您可能需要使用字符串資源 – Zoe

回答

2

希望這有助於。在Manifest.xml

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

這個字符串的

strings.xml 

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

使用引用在java中,你還可以通過使用setLongLabel (CharSequence longLabel)ShortcutInfo.BuildersetShortLabel(CharSequence)分配這一點。

+0

感謝您的答案。這工作。即使我不明白爲什麼我不應該對它們進行硬編碼,至少對於測試 – Daniele

+3

Manifest更喜歡Project中對任何「ID」的引用(當使用fb_id,fabric_id或map_id時)/ String(直接分配字符串時)。 –

相關問題