0

When the list in fragment A is clicked, it should pass date1 and ID to fragment B. I follow this but get a NullPointerException. I'm sure the date1 and ID holding a value since they display value when log get called.

Fragment A

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding listview_item_row in the result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the state's capital from this listview_item_row in the database.
                String ID =
                        cursor.getString(cursor.getColumnIndexOrThrow("_id"));
                String date1 = cursor.getString(cursor.getColumnIndexOrThrow("Date"));
                B fragment2 = new B();
                Bundle bundle = new Bundle();
                bundle.putString("ID", ID);
                bundle.putString("date1", date1);
                fragment2.setArguments(bundle);
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment1, fragment2);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                Log.e("TAG", ID);
                Log.e("TAG", date1);
            }
        });

Fragment B

 EditText name3;
 EditText date3;
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.updatepage1, container, false);
       Bundle bundle=this.getArguments();
       date=bundle.getString("date1");
       ID = bundle.getString("ID");
       RetrievePage(date,ID);
       return view;
    }

LogCat Error

11-22 23:09:30.896  29447-29447/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.project, PID: 29447
    java.lang.NullPointerException
            at com.example.project.project.B.onCreateView(B.java:69)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:545)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
Community
  • 1
  • 1
John
  • 684
  • 11
  • 35
  • What line causes the error ? – Mohammed Aouf Zouag Nov 22 '15 at 15:25
  • @JDev `date=bundle.getString("date1");` – John Nov 22 '15 at 15:26
  • Your fragments are placed in different activities, right? – Dania Nov 22 '15 at 15:27
  • You can't mix the `app.fragment`s with the `android.support.v4.app.fragment`s. – Mohammed Aouf Zouag Nov 22 '15 at 15:29
  • @JDev I'm using `import android.support.v4.app.Fragment;` in both fragment – John Nov 22 '15 at 15:30
  • In `FragmentA`, change `FragmentManager fragmentManager = getFragmentManager();` to `FragmentManager fragmentManager = getSupportFragmentManager();` – Mohammed Aouf Zouag Nov 22 '15 at 15:31
  • @John, yes in this case you will get a null pointer exception. What you're doing is replacing the fragment in Activity A with another Fragment and setting it's arguments. You're not sending any arguments to the Fragment in the another Activity. Thus, you got a NPE. – Dania Nov 22 '15 at 15:32
  • @Dania what should I do ? – John Nov 22 '15 at 15:33
  • Why don't you use intents? it suits the purpose of your app more, the way you're using is better when you want to replace the fragment in Activity A with another fragment without having any other activity. – Dania Nov 22 '15 at 15:33
  • I will post an answer that may help you. – Dania Nov 22 '15 at 15:34
  • @JDev `Error:(88, 51) error: cannot find symbol method getSupportFragmentManager()` – John Nov 22 '15 at 15:35
  • @John because you have to `import android.support.v4.app.FragmentManager;` , not `android.app.fragment` – Mohammed Aouf Zouag Nov 22 '15 at 15:36
  • @Dania Thanks. I cannot use intent because A is one of the navigation drawer item and I need to create two tabs. The B is one of the tab – John Nov 22 '15 at 15:36
  • @John, elaborate more, will activity B be opened when you click the list view in the fragment placed in Activity A? Or, will you replace the fragment in activity A with another one? – Dania Nov 22 '15 at 15:40
  • @JDev must change to `getSupportFragmentManager()` ? If I remove the line, it can open fragment B – John Nov 22 '15 at 15:40
  • @Dania If I remove `date=bundle.getString("date1");` and `ID = bundle.getString("ID");`, fragment B can be opened when listView in A is clicked – John Nov 22 '15 at 15:42
  • @John http://stackoverflow.com/a/33453400/1841194 – frogatto Nov 22 '15 at 15:45
  • Possible duplicate of [Communicating between more than 2 fragments](http://stackoverflow.com/questions/33452702/communicating-between-more-than-2-fragments) – frogatto Nov 22 '15 at 15:46
  • @HiI'mfrogatto Thanks, but the answer is unclear for me – John Nov 22 '15 at 15:47
  • @John Please refer to [this one](http://stackoverflow.com/questions/5194548/how-to-pass-data-between-fragments?lq=1). – frogatto Nov 22 '15 at 15:48
  • @John, try to use this , Fragment B= getSupportFragmentManager().findFragmentById(R.id.fragmentBId); Then set the arguments of fragment B. Let me know if it works. By this you're getting an instance of Fragment B and setting its arguments. – Dania Nov 22 '15 at 15:49
  • @Dania what fragmentBId refer to ? – John Nov 22 '15 at 15:52
  • This will work only if you declared your fragment in xml, fragmentBId is the id you assigned to the fragment tag in xml. Is it declared? – Dania Nov 22 '15 at 15:55
  • thanks everyone, I have solved it :) – John Nov 22 '15 at 16:04

1 Answers1

2

Thanks everyone, I have solved my own problem by using code below

Add this in fragment B and it works !

  Bundle bundle=this.getArguments();
            if(getArguments()!=null)
            {
                date=bundle.getString("date1");
                ID = bundle.getString("ID");
            }
John
  • 684
  • 11
  • 35