gpt4 book ai didi

java - 根据单击的项目膨胀操作模式菜单

转载 作者:行者123 更新时间:2023-12-02 04:43:56 25 4
gpt4 key购买 nike

我有一个 Activity ,在平板电脑上使用支票簿应用程序的主/详细流程(左侧为帐户,右侧为交易)。

当某个项目被用力按下时,我会使用上下文操作栏来允许用户在必要时编辑/删除该项目。我这样做是这样的:

@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);
}
}

如您所见,我获取了选定的项目,并将其作为标记传递给操作模式,以便我知道在必要时开始编辑或从数据库中删除哪个项目。

现在我有两个 Pane 布局,我无法正确设置我的操作模式回调。我想做这样的事情:

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;
}
};

但是,我得到了一个空指针异常,因为 Action 模式是在我调用 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)

我的帐户和交易都遇到相同的异常,唯一的区别是调用它们的方法。

最佳答案

我解决这个问题的方法是创建第二个 ActionMode.Callback 对象。我制作的第一个是用来处理帐户的,第二个是用来处理交易的。

它们看起来都像上面的那个,我可以这样调用它们:

// In Account long click
mActionMode = startSupportActionMode(mAccountActionModeCallback);

// In transaction long click
mActionMode = startSupportActionMode(mTransactionActionModeCallback);

然后,在每个回调中,我都能够扩展必要的菜单并处理必要的操作项。

关于java - 根据单击的项目膨胀操作模式菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29857762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com