我想在RecyclerView
项中添加复制Button
并复制文本,我添加了一个代码用于从RecyclerView
复制文本,但它不起作用。单击复制Button
后,应用程序崩溃。这是我复制文本的代码。
ClipboardManager clipboard = (ClipboardManager)holder.itemView.getContext().getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(Intent.EXTRA_TEXT, model.getShaData());
clipboard.setPrimaryClip(clip);
Toast.makeText(holder.itemView.getContext(), "Shayari Copied", Toast.LENGTH_SHORT).show();
我的日志猫
E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.indian.desishayari, PID: 346
java.lang.SecurityException: app.my.quotes from uid 10830 not allowed to perform READ_CLIPBOARD
at android.os.Parcel.createException(Parcel.java:1966)
at android.os.Parcel.readException(Parcel.java:1934)
at android.os.Parcel.readException(Parcel.java:1884)
at android.content.IClipboard$Stub$Proxy.setPrimaryClip(IClipboard.java:184)
at android.content.ClipboardManager.setPrimaryClip(ClipboardManager.java:104)
at app.indian.desishayari.ShayariAdapter$1.onClick(ShayariAdapter.java:50)
at android.view.View.performClick(View.java:6740)
at android.view.View.performClickInternal(View.java:6703)
at android.view.View.access$3400(View.java:803)
at android.view.View$PerformClick.run(View.java:26373)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:232)
at android.app.ActivityThread.main(ActivityThread.java:7172)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
Caused by: android.os.RemoteException: Remote stack trace:
at android.app.AppOpsManager.noteOp(AppOpsManager.java:2411)
at com.android.server.clipboard.ClipboardService.clipboardAccessAllowed(ClipboardService.java:653)
at com.android.server.clipboard.ClipboardService.setPrimaryClipInternal(ClipboardService.java:488)
at com.android.server.clipboard.ClipboardService.setPrimaryClipInternal(ClipboardService.java:423)
at com.android.server.clipboard.ClipboardService$ClipboardImpl.setPrimaryClip(ClipboardService.java:267)
如何解决这个问题? 请任何人帮忙...
你的复制代码是错误的,使用我认为有效的代码。我举例说,当您单击项目.
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", holder.yourTextView.getText().tostring());
if (clipboard == null || clip == null) return;
clipboard.setPrimaryClip(clip);
}
});