如何为Android应用程序制作应用程序快捷方式

时间:2016-11-03 12:20:31

标签: android android-intent android-activity

我已经阅读了android开发者官方页面的开发者指南,我也设法创建了快捷方式。但我想调用我自己的活动(明确),而不是(隐式),这在Android指南中给出了一个例子。这是https://developer.android.com/preview/shortcuts.html

的网址

我已经制作了这样的代码

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

    ShortcutInfo shortcut = new ShortcutInfo.Builder(this,"id1")

            .setShortLabel("Web site")
            .setLongLabel("Open the web site")


            .setIntent(new Intent(this,Shortcut.class))

    .build();

当我运行应用程序时,我收到此错误消息

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.murtaza.style2, PID: 4187
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.murtaza.style2/com.murtaza.style2.MainActivity}: java.lang.NullPointerException: intent's action must be set
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   Caused by: java.lang.NullPointerException: intent's action must be set
                      at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:111)
                      at android.content.pm.ShortcutInfo$Builder.setIntents(ShortcutInfo.java:987)
                      at android.content.pm.ShortcutInfo$Builder.setIntent(ShortcutInfo.java:968)
                      at com.murtaza.style2.MainActivity.onCreate(MainActivity.java:74)
                      at android.app.Activity.performCreate(Activity.java:6679)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Application terminated.

如何通过按快捷方式而不是调用任何其他应用来调用其他活动?

这不是一个3年的概念,在主屏幕上放置快捷方式,它刚刚宣布与Android 7.1。我想知道如何通过按下该快捷方式来调用活动。

0 个答案:

没有答案