6
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.

mihirjoshi
  • 12,161
  • 7
  • 47
  • 78

3 Answers3

2

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:

  1. Check that mClipboard is instantiated.
  2. I have tested the code with ClipData clip and setPrimaryClip lines commented and un-commented with no errors. However, you can test your code by adding these lines if you dont have them already.
  3. hasMimeType() and getPrimaryClipDescription() have been added to Android since API 11, so there is no reason for Samsung to disable them (without providing sufficient error message).
  4. 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.

  5. 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.

  6. 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.

Kalimah
  • 11,217
  • 11
  • 43
  • 80
2

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.

RBK
  • 2,481
  • 2
  • 30
  • 52
1

It's working in marshmallow. I tested on Nexus 5 running marshmallow. Please share the device you are using.

Here is the example:

import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
    EditText ed1,ed2;
    Button b1,b2;

    private ClipboardManager myClipboard;
    private ClipData myClip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed1=(EditText)findViewById(R.id.editText);
        ed2=(EditText)findViewById(R.id.editText2);

        b1=(Button)findViewById(R.id.button);
        b2=(Button)findViewById(R.id.button2);

        myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text;
                text = ed1.getText().toString();

                myClip = ClipData.newPlainText("text", text);
                myClipboard.setPrimaryClip(myClip);

                Toast.makeText(getApplicationContext(), "Text Copied",Toast.LENGTH_SHORT).show();
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myClipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML) ||
                        myClipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) ) {
                    ClipData abc = myClipboard.getPrimaryClip();
                    ClipData.Item item = abc.getItemAt(0);

                    String text = item.getText().toString();
                    ed2.setText(text);

                    Toast.makeText(getApplicationContext(), "Text Pasted", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
Marlon
  • 1,839
  • 2
  • 19
  • 42