カスタム 内にカスタム コンテキスト アクション バー (CAB) を作成していますWebView
。
私の要件は、最初の CAB から特定のオプションを選択すると、2 番目の CAB が開くようにすることです。これが最終目標です。
これを実現しましたが、選択が正しく機能していません。正しいように見えますが、ユーザーが他の 3 つのアイコンのいずれかをタップすると、次の結果になります。
ユーザーが 4 つの初期アイコンのいずれかを押した後に選択をタッチすると、null ポインター例外が原因でアプリがクラッシュします。
ユーザーがアクションを選択した後に残っている選択に触れると、アプリはアイコンを使用して CAB を作成したActionMode
(それを と呼びましょうfirstActionMode
) にアクセスして、CAB を無効化/更新しようとします。ただし、firstActionMode
色付きの CAB が作成されると破棄され (それを呼び出しますsecondActionMode
)、null ポインター例外が発生します。
clearFocus()
選択を解除するために、メソッド内で呼び出すとonDestroyActionMode(firstActionMode)
うまくいくことがわかりました。
private class CustomActionModeCallback extends ActionMode.Callback {
@Override
public void onDestroyActionMode(ActionMode mode) {
clearFocus();
}
}
ただし、これを実装すると、secondActionMode
その CAB を作成するときに選択が保持されません。
この点から色を選択すると、実際に目的の機能が生成されます。ただし、これは「機能します」が、(最も残念なことに)私の要件を満たしていません。によって作成されたCABが表示されているときに、選択を表示して機能secondActionMode
させる必要があります。
カスタム クラス (
WebView
) とそのネストされたActionMode
s のコードは次のとおりです。
public class CustomWebView extends WebView {
private ActionMode.Callback mActionModeCallback;
@Override
public ActionMode startActionMode(Callback callback) {
ViewParent parent = getParent();
if (parent == null) {
return null;
}
if (callback instanceof HighlightActionModeCallback) {
mActionModeCallback = callback;
} else {
mActionModeCallback = new CustomActionModeCallback();
}
return parent.startActionModeForChild(this, mActionModeCallback);
}
private class CustomActionModeCallback implements 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();
inflater.inflate(R.menu.first_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) {
// This method is called when the handlebars are moved.
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.copy:
// Do stuff
break;
case R.id.bookmark:
// Do stuff
break;
case R.id.highlight:
startActionMode(new HighlightActionModeCallback());
break;
case R.id.note:
// Do stuff
break;
default:
return false;
}
mode.finish(); // Action picked, so close the CAB
return true;
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
clearFocus(); // This is commented in the first four screens.
}
}
private class HighlightActionModeCallback implements 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();
inflater.inflate(R.menu.highlight_colors, 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) {
// This method is called when the handlebars are moved.
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) {
mode.finish(); // Action picked, so close the CAB
return true;
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
// Remove the selection highlight and handles.
clearFocus();
}
}
}
これはどのように修正できますか?どんな助けでも大歓迎です。