相关文章推荐
兴奋的开水瓶  ·  Spring Cloud ...·  1 年前    · 
瘦瘦的烈马  ·  sql进阶 - ...·  1 年前    · 
失恋的包子  ·  WPF ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Hi I'm developing an application that has 2 types of user: Admin and normal user; The admin obviously has more actions than the normal user, so I need to use dynamic Shortcuts, depending on the user the Shortcuts are created.

At this point everything is already codified and working. However, at the time of assigning an Intent , even to the MainActivity I receive an error.

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());

I also tried with:

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

But, in both I get the error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.motusk.Monit/com.motusk.Monit.activity.MainActivity}: java.lang.NullPointerException: intent's action must be set

In what I need help with is:

1) How to create a correct Intent to the MainActivity?

2) How to know which shortcut was pressed? (The process of depending on which shortcut was pressed to open a certain Fragment will be performed in the MainActivity, that's why I need to know which Shortcut was pressed).

As per the error message, your Intent must set an action with setAction:

intent.setAction("LOCATION_SHORTCUT");

You can then check the action in your Activity:

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.