无论我做什么,当我尝试在 switch case 中调用 toast 时,我总是收到类似 null object reference
的错误。 switch方法所在的类extends FragmentActivity
我尝试扩展 Fragment/v4。和 Activity 没有成功。
我还尝试将 getContext、getBaseContext、getAppliction();、getApplication().getBaseContext 等
作为上下文传递给 toast,但没有成功
如果我在我的 MainActivity 中创建一个公共(public) Toast 对象并像这样使用它 MainActivity.copyToast.show();
它有效,但这个解决方案看起来不太好。
我想像这样将它保留在一行中:Toast.makeText(this.getApplicationContext(), "Copied to clipboard.", Toast.LENGTH_SHORT).show();
全类:
public class CustomTextSelectionMenu extends Fragment implements android.view.ActionMode.Callback {
@Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
menu.removeItem(android.R.id.selectAll);
menu.removeItem(android.R.id.paste);
return true;
}
@Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
int selectionStart = editText.getSelectionStart();
int selectionEnd = editText.getSelectionEnd();
if (selectionEnd > selectionStart) {
Spannable str = editText.getText();
boolean exists = false;
StyleSpan[] styleSpans;
switch (item.getItemId()) {
//--------------------COPY----------------------------
case android.R.id.copy:
CharSequence charSequence = editText.getText().subSequence(selectionStart, selectionEnd);
ClipData clip = ClipData.newPlainText("simple text", charSequence);
MainActivity.clipboard.setPrimaryClip(clip);
Toast.makeText(getActivity().getApplicationContext(), "Copied to clipboard.", Toast.LENGTH_SHORT).show();
//MainActivity.copyToast.show();
break;
//--------------------BOLD----------------------------
case R.id.bold:
styleSpans = str.getSpans(selectionStart, selectionEnd, StyleSpan.class);
// If the selected text-part already has BOLD style on it, then
// we need to disable it
for (int i = 0; i < styleSpans.length; i++) {
if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD) {
str.removeSpan(styleSpans[i]);
exists = true;
}
}
// Else we set BOLD style on it
if (!exists) {
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStart, selectionEnd,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
editText.setSelection(selectionStart, selectionEnd);
break;
}
}
return true;
}
@Override
public void onDestroyActionMode(android.view.ActionMode mode) {
}
}
堆栈跟踪:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:110)
at org.m.muddzboy.QuoteCreator.CustomTextSelectionMenu.onActionItemClicked(CustomTextSelectionMenu.java:182)
at android.widget.Editor$SelectionActionModeCallback.onActionItemClicked(Editor.java:3228)
第 182 行指向这一点:
Toast.makeText(this.getApplicationContext(), "复制到剪贴板。", Toast.LENGTH_SHORT).show();
最佳答案
Toast 方法会导致很多问题,我们必须解决这个问题,这就是我回答这个问题的原因。
第一个上下文调用为 null 并像您的情况一样使您的应用程序崩溃。
如何修复它:在创建方法时保留来自上下文的引用
private Context mContext;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
mContext = getActivity()
}
第二个显示toast的时候可能是fragment被破坏怎么保证不会crash
public void showToast(String msg) {
if (YOR_FRAGMENT.this.isVisible() && msg != null & mContext != null)
Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
}
这会让你远离 toast 崩溃
希望对你有帮助
关于android - 在 switch case 中获取 Toasts 的空对象引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34312300/