What ever I do, I keep getting errors like null object reference
when I try to call a toast inside a switch case. The class where the switch method is in extends FragmentActivity
I have tried to extends Fragment/v4. and Activity without succses.
I also tried to pass the getContext, getBaseContext, getAppliction();, getApplication().getBaseContext etc
as context to the toast without succses
if I create a public Toast object in my MainActivity and use it like this MainActivity.copyToast.show();
It work, but this solution dosen't look good.
I want to keep it in one line like this: Toast.makeText(this.getApplicationContext(), "Copied to clipboard.", Toast.LENGTH_SHORT).show();
The whole class:
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) {
}
}
STACKTRACE:
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)
Line 182 points to this:
Toast.makeText(this.getApplicationContext(), "Copied to clipboard.", Toast.LENGTH_SHORT).show();