我有一项活动,在平板电脑上使用支票簿应用程序的主/详细信息流程(左侧的帐户,右侧的交易)。
当项目被按下时,我使用上下文操作栏允许用户在必要时编辑/删除项目。我这样做:
@Override
public void onTransactionLongClick(Transaction t) {
if(mActionMode == null){
// Start the CAB using the ActionMode.Callback already defined
mActionMode = startSupportActionMode(mActionModeCallback);
// Get name to set as title for action bar
mActionMode.setTitle(t.getDescription());
// Get account ID to pass as tag.
mActionMode.setTag(t);
}
}
@Override
public void onAccountLongSelected(AccountPrimitives ap) {
// Don't fire if the action mode is already active.
if(mActionMode == null){
// Start the CAB using the ActionMode.Callback already defined
mActionMode = startSupportActionMode(mActionModeCallback);
// Get name to set as title for action bar
mActionMode.setTitle(ap.getName());
// Get account ID to pass as tag.
mActionMode.setTag(ap);
}
}
正如您所看到的,我获取了所选项目并将其作为标记传递给操作模式,因此我知道在必要时从数据库开始编辑或删除哪个项目。
现在我有一个双窗格布局,我无法正确设置我的动作模式回调。我想做这样的事情:
private final ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
// Inflate based on tag type (account/transaction)
if(mActionMode.getTag() instanceof AccountPrimitives){
inflater.inflate(R.menu.account_context_menu, menu);
} else if(mActionMode.getTag() instanceof Transaction){
inflater.inflate(R.menu.transaction_context_menu, menu);
}
return true;
}
// Called each time the action mode is shown. Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_delete_account:
// The account that was selected is passed as the tag
// for the action mode.
showAccountDeleteAlertDialog((AccountPrimitives) mActionMode.getTag());
mode.finish(); // Action picked, so close the CAB
return true;
case R.id.action_delete_transaction:
showTransactionDeleteAlertDialog((Transaction) mActionMode.getTag());
mode.finish();
return true;
default:
return false;
}
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
但是,我得到一个空指针异常,因为在我调用mActionMode.setTag()之前启动了动作模式,所以我无法确定它的类型。在onXLongClick
方法中,我无法将setTag()移动到第一行,因为我也得到了一个NPE。
如何设置条件以为动作模式的特定菜单充气?
修改
当我长按一个项目时,我得到以下异常:
04-24 17:45:22.441 8117-8117/com.example.android.cashcaretaker E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.cashcaretaker, PID: 8117
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.support.v7.view.ActionMode.getTag()' on a null object reference
at com.example.android.cashcaretaker.AccountsActivity$1.onCreateActionMode(AccountsActivity.java:58)
at android.support.v7.app.ActionBarActivityDelegateBase$ActionModeCallbackWrapper.onCreateActionMode(ActionBarActivityDelegateBase.java:1451)
at android.support.v7.internal.app.WindowDecorActionBar$ActionModeImpl.dispatchOnCreate(WindowDecorActionBar.java:1015)
at android.support.v7.internal.app.WindowDecorActionBar.startActionMode(WindowDecorActionBar.java:510)
at android.support.v7.app.ActionBarActivityDelegateBase.startSupportActionMode(ActionBarActivityDelegateBase.java:570)
at android.support.v7.app.ActionBarActivity.startSupportActionMode(ActionBarActivity.java:225)
at com.example.android.cashcaretaker.AccountsActivity.onTransactionLongClick(AccountsActivity.java:177)
at com.example.android.cashcaretaker.TransactionFragment$1.onItemLongClick(TransactionFragment.java:72)
at android.widget.AbsListView.performLongPress(AbsListView.java:3121)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3070)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我对帐户和事务都有相同的例外,唯一的区别是调用它们的方法。
答案 0 :(得分:2)
我解决这个问题的方法是创建第二个ActionMode.Callback
对象。第一个我处理帐户,第二个处理交易。
它们看起来就像上面那样,我可以这样称呼它们:
// In Account long click
mActionMode = startSupportActionMode(mAccountActionModeCallback);
// In transaction long click
mActionMode = startSupportActionMode(mTransactionActionModeCallback);
然后,在每个回调中,我都能够扩充必要的菜单并处理必要的操作项。