if (mClipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) ||
mClipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML))
getPrimaryClipDescription()
is null in few Android 6.0.1.
Attempt to invoke virtual method 'boolean android.content.ClipDescription.hasMimeType(java.lang.String)' on a null object reference
Update
Please try on devices like Samsung Galaxy S5 and Note 4.
I tested this code on Galaxy S6 - 6.0.0 image using Genymotion emulator and I did not receive any errors.
// Copy to clipbaord
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
// ClipData clip = ClipData.newPlainText("Label", "TEXT");
// clipboard.setPrimaryClip(clip);
if (clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) ||
clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML))
{
Log.d("Copied!", "Copied!");
}
Few suggestions:
Try to add this code before your main code:
if (!(mClipboard .hasPrimaryClip()))
This will ensure that the clipboard has a primary clip that you can handle.
Check this link from Android website: https://developer.android.com/guide/topics/text/copy-paste.html It has good information and code on copying and pasting content.
Finally, contact Samsung. You can post a question on Samsung Developers Community http://developer.samsung.com/community to check the source of the problem since this issue is specific to Samsung devices.
Before attempting that method you can check it has PrimaryClip or not.
if(mClipboard.hasPrimaryClip() && (mClipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) || mClipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML)))
that can be a good way to avoid null pointer exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With