【问题标题】:Dynamic Shortcuts to open Fragments in Android在 Android 中打开 Fragments 的动态快捷方式
【发布时间】:2017-08-09 19:29:42
【问题描述】:

您好,我正在开发一个具有 2 种用户类型的应用程序:管理员和普通用户;管理员显然比普通用户有更多的操作,所以我需要使用动态快捷方式,这取决于创建快捷方式的用户。

此时,一切都已经编好并开始工作了。但是,在分配Intent 时,即使是MainActivity 我也会收到错误消息。

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.motusk.Monit", "com.motusk.Monit.activity.MainActivity"));

shortcut.add(new ShortcutInfo.Builder(this, "ub")
   .setShortLabel("Location")
   .setLongLabel("Location")
   .setIcon(Icon.createWithResource(this, R.drawable.bg))
   .setIntent(intent)
   .build());

我也试过了:

Intent intent = new Intent(this, MainActivity.class);

但是,在这两个中我都得到了错误:

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.motusk.Monit/com.motusk.Monit.activity.MainActivity}:java.lang.NullPointerException:必须设置意图的操作

我需要帮助的是:

1) 如何创建一个正确的IntentMainActivity

2) 如何知道按下了哪个快捷键? (取决于按下哪个快捷键打开某个Fragment的过程会在MainActivity中进行,所以我需要知道按下了哪个快捷键)。

【问题讨论】:

    标签: android android-fragments android-intent dynamic shortcuts


    【解决方案1】:

    根据错误消息,您的 Intent 必须使用setAction 设置操作:

    intent.setAction("LOCATION_SHORTCUT");
    

    然后您可以在 Activity 中检查操作:

    String action = getIntent() != null ? getIntent().getAction() : null;
    if ("LOCATION_SHORTCUT".equals(action)) {
      // Show the correct fragment
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多