【问题标题】:Why can't pass data between fragment?为什么片段之间不能传递数据?
【发布时间】:2015-11-22 15:23:09
【问题描述】:

当点击fragment A 中的list 时,它应该将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)

【问题讨论】:

  • 哪一行导致错误?
  • @JDev date=bundle.getString("date1");
  • 你的fragment放在不同的activity中吧?
  • 您不能将app.fragments 与android.support.v4.app.fragments 混合使用。
  • @JDev 我在两个片段中都使用import android.support.v4.app.Fragment;

标签: android android-fragments nullpointerexception bundle


【解决方案1】:

谢谢大家,我用下面的代码解决了我自己的问题

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

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

【讨论】:

    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 2011-07-08
    • 1970-01-01
    相关资源
    最近更新 更多