我有一个包含许多CardView的RecyclerView。每个CardView都有一个唯一的ID。
我需要为每个CardView添加一个按钮,以便当用户点击此按钮时,他可以以" www.domain.com/xx=ID"的格式共享一个简单的链接;通过Whatsapp或messenger或任何其他应用程序。
我尝试了以下代码:
holder.myBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "text to be shared";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
但是我收到以下错误:
D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
V/FA: Inactivity, disconnecting from the service
D/AndroidRuntime: Shutting down VM
E/UncaughtException: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1597)
at android.app.ContextImpl.startActivity(ContextImpl.java:1584)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:337)
at com.myapp.myapp_rental.PropertiesAdapter$4.onClick(PropertiesAdapter.java:229)
at android.view.View.performClick(View.java:5181)
at android.view.View$PerformClick.run(View.java:20887)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
D/FA: Logging event (FE): _ae, Bundle[{_o=crash, _sc=ListingActivity, _si=-3768936060483324961, timestamp=1484388033695, fatal=1}]
V/FA: Using measurement service
V/FA: Connecting to remote service
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.myapp_rental, PID: 20082
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1597)
at android.app.ContextImpl.startActivity(ContextImpl.java:1584)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:337)
at com.myapp.myapp_rental.PropertiesAdapter$4.onClick(PropertiesAdapter.java:229)
at android.view.View.performClick(View.java:5181)
at android.view.View$PerformClick.run(View.java:20887)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
D/ResourcesManager: creating new AssetManager and set to /data/app/com.google.android.gms-2/base.apk
W/ResourcesManager: Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
W/ResourcesManager: Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
答案 0 :(得分:1)
对我来说,有一个简单的解决方案,你需要做的事情。
在Cardview的布局中,您可以添加包含Easy Share Action的工具栏: CardView toolbars
在您的RecyClerView中,每次调用onBindViewHolder时,您都可以在菜单https://developer.android.com/training/sharing/shareaction.html中指定包含所需链接和ID的项目。
如果您在使用RecyclerView和CardView时遇到问题,请按照以下指南操作:https://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156
修改:如果您想通过WhatsApp分享,请参阅:https://www.whatsapp.com/faq/en/android/28000012或使用https://developers.facebook.com/docs/messenger/android
等API我希望我有所帮助。 :)
答案 1 :(得分:0)
您通常看到的共享按钮大多数是ImageViews
,并为其分配了onClick
属性。如果您想添加分享按钮以分享卡片上的内容,只需在卡片布局中添加ImageView
,然后在ImageView
添加onClickListener
并添加Intent
待部署。
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
您还可以添加一个setType函数,可以将其设置为共享链接。