gpt4 book ai didi

java - android.util.AndroidRuntimeException : Calling startActivity() from outside of an Activity

转载 作者:行者123 更新时间:2023-11-29 07:26:53 25 4
gpt4 key购买 nike

检查下面我的代码,。此代码在我的适配器中。但问题是我的手机完全支持应用程序和共享,但较低版本的手机不支持它在点击共享时强制关闭。

 share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

String shareBody = "\t\t DIL KI BAAT \n\n" +sayari[position].toString().trim()+"\n\n\n"+ "https://play.google.com/store/apps/details?id="
+ appPackageName;

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
context.startActivity(Intent.createChooser(sharingIntent, context.getResources()
.getString(R.string.app_name)));
}
});

最佳答案

您需要添加此标志不是为了共享 Intent ,而是为了您开始 Activity 的 Intent :

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

Intent startIntent = Intent.createChooser(sharingIntent, context.getResources().getString(R.string.app_name));
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);

关于java - android.util.AndroidRuntimeException : Calling startActivity() from outside of an Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50636808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com