片段参数为空

时间:2015-03-12 11:00:12

标签: java android android-fragments bundle

我正在尝试将数据从一个片段发送到另一个片段,我正在使用bundle。但是,每当我尝试从第二个片段中的该包中获取任何信息时,我会收到一条错误消息,说我正在尝试获取一个空对象。我在创建第二个片段之前已经设置了第二个片段的参数,并且在发送之前我还要向它添加信息。我无法找出问题所在。这是主片段中的接口代码,它应该打开详细信息片段

 public interface ListClickHandler {

        public void onlistElementClicked ( Bundle args); //we'll have to override it in the parent activity.
    }//end interface.

    public void onAttach(Activity activity) {
        super.onAttach(activity);


        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (ListClickHandler) activity;
        }//end try

        catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement ListClickHandler interface");
        }//end catch
    }

另外,我在两个地方创建捆绑包,一次在主片段中,其中包含一个列表,如果点击任何项目创建了捆绑包,则将信息添加到捆绑包中,并将该捆绑包传递给内部的方法接口如下,

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_todo_list, container, false);
        mSimpleCursorAdapter=new SimpleCursorAdapter(getActivity(),R.layout.notes_row,null, from, to,0);
        getLoaderManager().initLoader(LOADER_ID, null, this); //once this is done onCreateLoader will be called.
        ListView listView = (ListView) rootView.findViewById(R.id.notes_list); //findViewById must be called using the rootView because we are inside a fragment.
        listView.setAdapter(mSimpleCursorAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Cursor cursor = mSimpleCursorAdapter.getCursor();
                if (cursor != null && cursor.moveToPosition(position)) {

                    String category= cursor.getString(1);
                    String summary= cursor.getString(2);
                    String description=cursor.getString(3);
                    long id= cursor.getLong(cursor.getColumnIndex(NotesContract.NotesTable._ID));
                    int locationId= cursor.getInt(cursor.getColumnIndex(NotesContract.NotesTable.COLUMN_LOCATION));

                    String [] retrievedData= {category, summary, description};


                    if (getActivity().findViewById (R.id.fragment_container)!=null){
                        //two pane layout:
                        Bundle args = new Bundle();
                        args.putStringArray("data",retrievedData);
                        /*args.putInt("update", 1);*/
                        args.putLong("id", id);
                        args.putInt("locationId", locationId);
                        mCallback.onlistElementClicked(args );/*this is available in the parent activity*/
                    }

                    else {
                       // one pane layout:

                        Intent intent = new Intent(getActivity(), NotesDetails.class);
                        intent.putExtra(Intent.EXTRA_TEXT, retrievedData);
                        /*intent.putExtra("update", 1); */ //to indicate that the query should be update not insert.
                        intent.putExtra("id", id);
                        intent.putExtra("locationId", locationId); //whether it is 0 or 1
                        startActivity(intent);
                    }


                }//end outer cursor if.
            }
        });

        return rootView;
    }

当我选择菜单中的某些项目如下所示时,我创建并调用该包的第二个位置是主活动(包含主片段),

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {//open the settings activity to enable the user to change the settings.
            //open settings activity via intent here.
            startActivity(new Intent (this, Settings.class));
            return true;
        }

        if (id==R.id.text_note){  //open the details activity where the user can enter their notes and save it.
            if (twoPane) {
                args.putBoolean("location", false);
                mCallBack.onlistElementClicked(args);
            }
            else {
                Intent intent = new Intent(this, NotesDetails.class);
                startActivity(intent);
                return true; //this line is necessary
            }
        }//end if

        if (id==R.id.location_note)
        {
            if (twoPane) {
                args.putBoolean("location", true);
                mCallBack.onlistElementClicked(args);
            }
            else {
                //prepare intent here:
                Intent intent = new Intent(this, NotesDetails.class);
                intent.putExtra("location", true);
                startActivity(intent);
            }
        }

        return super.onOptionsItemSelected(item);
    }

这是我在主要活动

中覆盖onlistElementClicked的方法
@Override
    public void onlistElementClicked(Bundle args) {

        DetailsFragment detailsFragment = new DetailsFragment();
        detailsFragment.setArguments(args); 

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, detailsFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();


    }//end interface method.

这就是我如何获取细节片段(应该从主片段打开的片段)的参数内的信息。

Bundle args=this.getArguments();

之后我使用args来获取包中的任何信息,但是我收到了之前提到的错误。

任何人都可以帮帮我吗?我在网上查了几个解决方案,对我来说没什么用。

谢谢。

2 个答案:

答案 0 :(得分:0)

您应该像这样分配Bundle的值:

public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    youtBandGlobalMember = getArguments();
}

答案 1 :(得分:0)

Bundle args=this.getArguments();
     实际上,我没有在任何方法中调用它,它在片段类的主体中调用,并将其值赋给全局变量。这是错的吗?

现在还为时过早。成员变量在构造对象时初始化,并且在您可以在片段对象上调用setArguments()之前。

getArguments()调用推迟到片段中的on...()生命周期回调之一。