在Fragment中共享文件时,应用程序崩溃

时间:2018-09-04 15:37:30

标签: java android android-fragments

我有一个与MainActivity相连的片段。它显示图像。用户必须共享图像,但是单击共享按钮时应用程序崩溃。

这是片段代码:

Context mcontext = getActivity();
case R.id.item_share: {

    final Uri uri = FileProvider.getUriForFile(mcontext, "com.whats.insta", file);

    final Intent intent = ShareCompat.IntentBuilder
            .from((Activity)mcontext)
            .setType("image/jpg")
            .setStream(uri)
            .createChooserIntent()
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    mcontext.startActivity(intent);*/

    break;
}

崩溃日志

09-05 10:41:05.410 26011-26011/com.whats.insta E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.whats.insta, PID: 26011
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.app.Activity.getPackageName()' on a null object reference
        at android.support.v4.app.ShareCompat$IntentBuilder.<init>(ShareCompat.java:216)
        at android.support.v4.app.ShareCompat$IntentBuilder.from(ShareCompat.java:210)
        at com.whats.insta.ui.imageslider.imagedetails.ImageDetailsFragment.onOptionsItemSelected(ImageDetailsFragment.java:267)
        at android.support.v4.app.Fragment.performOptionsItemSelected(Fragment.java:2476)
        at android.support.v4.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:3343)
        at android.support.v4.app.FragmentController.dispatchOptionsItemSelected(FragmentController.java:347)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:413)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108)
        at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:63)
        at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:203)
        at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:780)
        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
        at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150)
        at android.view.View.performClick(View.java:6261)
        at android.widget.TextView.performClick(TextView.java:11185)
        at android.view.View$PerformClick.run(View.java:23752)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

1 个答案:

答案 0 :(得分:0)

您必须检查隐式意图是否至少解决了一项活动,否则应用程序崩溃。

// Verify the intent will resolve to at least one activity
if (mContext != null && intent.resolveActivity(getPackageManager()) != null) {
    startActivity(chooser);
}