如何添加共享按钮在recyler视图项目点击



我正在制作一个简单的应用程序,通过API获取数据,我想在我的recyler视图上添加一个按钮来共享图像的链接,但当我单击共享按钮时,我的应用程序崩溃,出现以下错误我的recylerview适配器:-

public class Adapter  extends RecyclerView.Adapter<Adapter.ViewHolder> {
Context context;
ArrayList<Model> modelArrayList;
public Adapter(Context context, ArrayList<Model> modelArrayList) {
this.context = context;
this.modelArrayList = modelArrayList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view  = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);

return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String memeUrl = modelArrayList.get(position).getUrl();
holder.setImage(memeUrl);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent sharing = new Intent(Intent.ACTION_SEND);
sharing.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sharing.setType("text/plain");
//  String sharebody = "Quizz time beautiful app";
String subject = "Hey watch this cool meme Click this link "+memeUrl+"nn";
sharing.putExtra(Intent.EXTRA_TEXT, subject);
context.startActivity(Intent.createChooser(sharing, "Share using"));
}
});
}
@Override
public int getItemCount() {
return modelArrayList.size();
}
public  class ViewHolder extends RecyclerView.ViewHolder{
ImageView memeImage;
Button button;
public ViewHolder(@NonNull View itemView) {
super(itemView);
memeImage = itemView.findViewById(R.id.imageView);
button = itemView.findViewById(R.id.button);
}
void  setImage(String link){
Glide.with(context).load(link).into(memeImage);
}
}

}

我的主要活动:-

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
Adapter adapter;
ArrayList<Model> arrayList;
ProgressDialog dialog ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recylerview_id);
arrayList = new ArrayList<>();
dialog = new ProgressDialog(this);
dialog.setMessage("Getting Memes...");
dialog.setCancelable(false);
dialog.show();

String url = "https://meme-api.herokuapp.com/gimme/30";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.v("MEMERESPONSE",response.toString());
try {
JSONArray array = response.getJSONArray("memes");
Log.v("Arraycheckingxx", array.toString());
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
// Log.v("Arraychecking"+i, jsonObject.toString());
String url = jsonObject.getString("url");
Log.v("url"+i, url);
Model model = new Model(url);
arrayList.add(model);
}
Log.v("Arraychecking", String.valueOf(arrayList.size()));
adapter = new Adapter(getApplicationContext(), arrayList);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
dialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});



MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);

}

}

但当我点击按钮时,我的应用程序崩溃,出现以下错误

ndroid.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:922)
at android.app.ContextImpl.startActivity(ContextImpl.java:898)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:389)
at com.choudhary.memegram.Adapter$1.onClick(Adapter.java:53)
at android.view.View.performClick(View.java:6608)
at android.view.View.performClickInternal(View.java:6585)
at android.view.View.access$3100(View.java:785)
at android.

adapter = new Adapter(getApplicationContext(), arrayList);更改为

adapter = new Adapter(MainActivity.this, arrayList);

您正在适配器中使用应用程序上下文,但您需要活动的上下文,因此不需要添加标志

最新更新