2017-03-18 43 views
0

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

此时一切都已经编纂和工作。但是,在分配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: Unable to start activity ComponentInfo{com.motusk.Monit/com.motusk.Monit.activity.MainActivity}: java.lang.NullPointerException: intent's action must be set

在我需要帮助的是:

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

2)如何知道哪个快捷键被按下? (取决于按哪个快捷方式打开某个Fragment的过程将在MainActivity中执行,这就是为什么我需要知道哪个快捷方式被按下的原因)。

回答

2

按照错误信息,你的意图必须设置一个行动setAction

intent.setAction("LOCATION_SHORTCUT"); 

就可以检查你的活动的行动:

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