Sujit Maharjan
Sujit Maharjan

Reputation: 29

Can't get bundle argument in fragment from activity

I am very new as android developer(currently PHP programmer).I am trying learn android by following various tutorials from google developer and other websites.

My problem is that i can't get bundle arguments in my fragment which was set in my actvity.

Here is the method of my HomeActivity:

public void SelectItem(int possition) {
    Fragment fragment = new FragmentImageList();
    Bundle bundle = new Bundle();
    if (possition <= 3) {

        bundle.putString("test_string", "Hello test");
        fragment.setArguments(bundle);
        FragmentManager frgManager = getFragmentManager();
        frgManager.beginTransaction().replace(R.id.image_list_frame, fragment)
                .commit();
    }

}

Here is my FragmentImageList Fragment:

public FragmentImageList() {

}

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


    String strtext=getArguments().getString("test_string");
    Log.d("test string",strtext);
    Bundle bundle = this.getArguments();
    Log.d(TAG,  bundle.getString("test_string"));
    View view = inflater.inflate(R.layout.fragment_image_list, container,
            false);
    GridView gridView = (GridView) view.findViewById(R.id.gridView);
    gridView.setAdapter(new GridViewAdapter());
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            String url = (String) view.getTag();
            Log.d(TAG,  url);
            Intent intent = new Intent(view.getContext(), DetailActivity.class);
            intent.putExtra("EXTRA_IMAGE", url);
            startActivity(intent);


        }
    });
    return view;
}

My Error log

    07-04 22:16:49.908    1200-1200/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app
    07-04 22:16:49.908    1200-1200/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
    07-04 22:16:50.033  14550-14550/? E/PhotosPlugin﹕ Loading PhotosPlugin
    07-04 22:16:50.480  14620-14620/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.sujit.wallpapersnepal, PID: 14620
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sujit.wallpapersnepal/com.sujit.wallpapersnepal.HomeActivity}: 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:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    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:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
    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.sujit.wallpapersnepal.FragmentImageList.onCreateView(FragmentImageList.java:37)
    at android.app.Fragment.performCreateView(Fragment.java:2053)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
    at android.app.BackStackRecord.run(BackStackRecord.java:834)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
    at android.app.Activity.performStart(Activity.java:6005)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                at android.app.ActivityThread.access$800(ActivityThread.java:151)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5254)
                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:903)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
    07-04 22:16:52.006  14729-14729/? E/SysUtils﹕ ApplicationContext is null in ApplicationStatus
    07-04 22:16:52.782  14820-14820/? E/ObjectHelper﹕ Can't find method:setCompatibilityInfo
    07-04 22:16:54.497  15063-15063/? E/SysUtils﹕ ApplicationContext is null in ApplicationStatus
    07-04 22:16:54.613    1200-1200/? E/NetworkScheduler.SchedulerReceiver﹕ Invalid parameter app

String strtext=getArguments().getString("test_string"); is not working.I also tried Bundle bundle = this.getArguments(); Log.d(TAG, bundle.getString("test_string")); but it's also not working. However, if i comment these bundle lines then app runs fine without errors. This "test_string" is just a test argument i am trying to make it work. So, i want to know what mistake i have done that's giving me this error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sujit.wallpapersnepal/com.sujit.wallpapersnepal.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

Edited: I am trying to run this app in my Nexus 5 android version 5.1.1

Upvotes: 1

Views: 3660

Answers (2)

akash89
akash89

Reputation: 891

Can you try changing this line

Bundle bundle = this.getArguments();

to

String value = getArguments().getString("key"); Log.i("ANDROID",value);

Also I am not sure, whether you should be getting the bundle and then inflating the view, do the other way round, first inflate the view and then take the bundle

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(MainActivity.TAG,"Fragment onCreateView()");
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        String value = getArguments().getString("key");
        Log.i("ANDROID",value);

        return rootView;

    }

Upvotes: 0

user4989692
user4989692

Reputation: 1609

Consider using the factory methods as like in the fragments guide (near the bottom). The reasons for doing so can be found here.

In your FragmentImageList fragment add the following static method.

public static FragmentImageList newInstance(String string) {
    FragmentImageList f = new FragmentImageList();

    // Supply index input as an argument.
    Bundle args = new Bundle();
    args.putString("test_string", string);
    f.setArguments(args);

    return f;
}

and amend your onCreate() method like so

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString("test_string");
    }
}

And in your SelectItem method,

Fragment fragment = FragmentImageList.newInstance("Hello test");

Upvotes: 2

Related Questions