无论我做什么,我都会遇到错误,比如null object reference
当我尝试在开关盒内调用吐司时.switch方法所在的类扩展了FragmentActivity
我试图扩展Fragment/v4.和没有succses的活动.我也尝试将getContext, getBaseContext, getAppliction();, getApplication().getBaseContext etc
as上下文传递给toast而没有succses
如果我在我的MainActivity中创建一个公共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(), "Copied to clipboard.", Toast.LENGTH_SHORT).show();
吐司方法可能会导致许多问题,我们必须解决它,这就是为什么我回答这个问题.
第一个上下文称为null,并在您的情况下崩溃您的应用程序.
如何解决它:在create方法上保持对上下文的引用
private Context mContext; @Override public void onCreate(@Nullable Bundle savedInstanceState) { mContext = getActivity() }
第二次显示吐司时可能会破坏片段如何确保它不会崩溃
public void showToast(String msg) { if (YOR_FRAGMENT.this.isVisible() && msg != null & mContext != null) Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show(); }
这将使你远离吐司崩溃
希望这个帮助