尝试将数据从活动传递到片段时发生NullException

时间:2018-09-20 13:42:56

标签: android nullpointerexception fragment bundle

有些奇怪:我正在尝试将数据从Profile活动传递到VewPostFragment: 活动中的代码为:

private String s=" ";
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        Log.d(TAG, "onCreate: started.");

        ProfileUserName = (TextView) findViewById(R.id.display_name);

        test();
        ViewPostFragment fragment = ViewPostFragment.newInstance("Your value");
    }
 private void test()
    {
        ViewPostFragment mFragment=new ViewPostFragment();
                                Bundle args=new Bundle();
                                args.putString("PostKeyS" , s);
                                mFragment.setArguments(args);
    }

我检查“ s”数据是否不为空

该片段中的代码是:

 @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
    {

        View view=inflater.inflate(R.layout.fragment_view_post,container,false);


        mBackArrow=(ImageView) view.findViewById(R.id.backArrow);



        String value = getArguments().getString("PosKeyS");
        Toast.makeText(getActivity(), "TThe code is the same?" + value, Toast.LENGTH_SHORT).show();
        return view;

    }
public static ViewPostFragment newInstance(String s) {
        ViewPostFragment fragment = new ViewPostFragment();
        Bundle args = new Bundle();
        args.putString("PostKeyS", s);
        fragment.setArguments(args);
        return fragment;
    }

,我总是出错: 致命异常:主要                   流程:com.example.android.bluesky,PID:22380                   java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.android.bluesky / com.example.android.bluesky.Profile.ProfileActivity}:java.lang.NullPointerException:尝试调用虚拟方法'java.lang.String android.os.Bundle.getString(java.lang.String)'上的空对象引用                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2406)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)                       在android.app.ActivityThread.access $ 1200(ActivityThread.java:152)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1341)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:135)                       在android.app.ActivityThread.main(ActivityThread.java:5538)                       在java.lang.reflect.Method.invoke(本机方法)                       在java.lang.reflect.Method.invoke(Method.java:372)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:958)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)                    原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String android.os.Bundle.getString(java.lang.String)'                       在com.example.android.bluesky.ViewPostFragment.onCreateView(ViewPostFragment.java:91)                       在android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)                       在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)                       在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)                       在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)                       在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)                       在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)                       在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)                       在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)                       在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)                       在android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)                       在android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)                       在android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)                       在android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)                       在android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)                       在com.example.android.bluesky.Profile.ProfileActivity.onStart(ProfileActivity.java:115)                       在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1224)                       在android.app.Activity.performStart(Activity.java:6032)                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)                       在android.app.ActivityThread.access $ 1200(ActivityThread.java:152)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1341)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:135)                       在android.app.ActivityThread.main(ActivityThread.java:5538)                       在java.lang.reflect.Method.invoke(本机方法)                       在java.lang.reflect.Method.invoke(Method.java:372)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:958)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753) I / Process:正在发送信号。 PID:22380 SIG:9 应用已终止。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您应该为此使用newInstance方法

首先,您必须在片段中添加此方法

public static ViewPostFragment newInstance(String s) {
    ViewPostFragment fragment = new ViewPostFragment();
    Bundle args = new Bundle();
    args.putString("PostKeyS", s);
    fragment.setArguments(args);
    return fragment;
}

然后您必须在这样的活动中发送数据

ViewPostFragment fragment = ViewPostFragment.newInstance("Your value");

最后,您可以像这样在片段中获取数据

String value = getArguments().getString("PosKeyS");

答案 1 :(得分:0)

我已要求您发布其余ViewPostFragment,因为我想问题出在您提供的代码之外(我将在需要时更新此答案)。

所提供的上下文与传递数据的方式没有直接关系。

正如您所说,您正在尝试使用Toast方法的onCreateView()

由于这是一个片段,请改为在onViewCreated()(这还取决于您的片段从支持版本或(不建议使用的)框架扩展的Fragment超类

我还建议您阅读这些3分钟的文章,以帮助您清除传递数据的方式: