使用grantUriPermission错误的ACTION_SEND崩溃活动

问题描述 投票:2回答:2

当我调用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:

<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"));

活动已关闭,但我仍然看到一个共享框。

感谢您的帮助,如果您需要其他信息,请写评论

android android-activity action
2个回答
0
投票

解决了我在onPause中完成()所以当分享应用程序时暂停并完成...


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上测试过。

© www.soinside.com 2019 - 2024. All rights reserved.