为什么不能在片段之间传递数据?

时间:2015-11-22 15:23:09

标签: android android-fragments nullpointerexception bundle

点击list A中的fragment后,它应该会将date1ID传递给fragment B.我会关注this但是获得NullPointerException。我确定date1ID持有一个值,因为它们会在调用log时显示值。

片段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);
            }
        });

片段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错误

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)

1 个答案:

答案 0 :(得分:2)

谢谢大家,我已经通过使用下面的代码解决了我自己的问题

在片段B中添加它,它可以工作!

  Bundle bundle=this.getArguments();
            if(getArguments()!=null)
            {
                date=bundle.getString("date1");
                ID = bundle.getString("ID");
            }