Reputation: 101
Something weird: i'm trying to pass data from Profile activity to VewPostFragment: The code in activity is:
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);
}
I check if the "s" data is not null
in the fragment my code is:
@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;
}
and i always get error: FATAL EXCEPTION: main Process: com.example.android.bluesky, PID: 22380 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.bluesky/com.example.android.bluesky.Profile.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2406) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466) at android.app.ActivityThread.access$1200(ActivityThread.java:152) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5538) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:958) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at com.example.android.bluesky.ViewPostFragment.onCreateView(ViewPostFragment.java:91) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245) at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248) at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200) at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597) at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177) at com.example.android.bluesky.Profile.ProfileActivity.onStart(ProfileActivity.java:115) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1224) at android.app.Activity.performStart(Activity.java:6032) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466) at android.app.ActivityThread.access$1200(ActivityThread.java:152) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5538) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:958) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753) I/Process: Sending signal. PID: 22380 SIG: 9 Application terminated.
Any ideas?
Upvotes: 0
Views: 307
Reputation: 4127
I've asked you to post the rest of your ViewPostFragment
because my guess is the issue is outside of the code you provided (I'll update this answer if needed).
From the provided context is not directly related the way you are passing the data around.
As you stated you are trying to do the Toast
on the onCreateView()
method.
Since this is a fragment do it on the onViewCreated()
instead (this also depends on what Fragment
superclass your fragment extends from the support version or the (deprecated) framework.
I also recommend you reading these 3-minute articles to help you clear up the way you pass data around:
Upvotes: 0
Reputation: 447
You should use newInstance method for this
firstly you must add this method in your fragment
public static ViewPostFragment newInstance(String s) {
ViewPostFragment fragment = new ViewPostFragment();
Bundle args = new Bundle();
args.putString("PostKeyS", s);
fragment.setArguments(args);
return fragment;
}
then you must send the data in the activity like this
ViewPostFragment fragment = ViewPostFragment.newInstance("Your value");
finally you can get the data in your fragment like this
String value = getArguments().getString("PosKeyS");
Upvotes: 0