当我致电ACTION_SEND时,这会显示一个共享框,但活动已关闭并显示错误
try {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String link = preferences.getString("speed", "");
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "subject");
String sAux = "\nText:\n\n";
sAux = sAux + link + "\n\n";
i.putExtra(Intent.EXTRA_TEXT, sAux);
startActivity(Intent.createChooser(i, "Share Link"));
} catch (Exception e) {
e.toString();
}
我已使用此配置活动:
<activity android:name=".Settings" android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
完整的错误代码是:
W/Intent: Failure when grantUriPermission
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.net.Uri.writeToParcel(android.os.Parcel, int)' on a null object reference
at android.app.ActivityManagerProxy.grantUriPermission(ActivityManagerNative.java:4830)
at android.content.Intent.migrateExtraStreamToClipData(Intent.java:9195)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1529)
at android.app.Activity.startActivityForResult(Activity.java:4391)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4335)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at android.app.Activity.startActivity(Activity.java:4697)
at android.app.Activity.startActivity(Activity.java:4665)
at test.app.Settings.Share(Settings.java:56)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22465)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6577)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
at android.app.ActivityManagerProxy.grantUriPermission(ActivityManagerNative.java:4830)
在第56行,我有:startActivity(Intent.createChooser(i, "Share Link"));
活动已关闭,但我仍然看到一个共享框。
感谢您的帮助,如果您需要其他信息写评论
答案 0 :(得分:0)
解决了我在onPause中完成()所以当共享应用程序时,请暂停并完成...
答案 1 :(得分:0)
我有同样的错误。就我而言,活动没有关闭,但是错误在错误日志中。这仅发生在Android 7上。我可以通过使用具有以下设置的Intent来摆脱此错误:
Intent share = new Intent(Intent.ACTION_SEND);
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
share.setDataAndType(new Uri.Builder().build(), "message/rfc822");
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
share.putExtra(Intent.EXTRA_SUBJECT, "Here the subject");
share.putExtra(Intent.EXTRA_TEXT, "https://www.example.com/share?r=123456789");
startActivity(Intent.createChooser(share, "Create..."));
在Android 5.1、7和9上进行了测试。