Search code examples
androidandroid-fragmentsnullpointerexceptionbundle

Why can't pass data between fragment?


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)

Solution

  • 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");
                }