3

所以我把这段代码fragment合二为一(Info_Frag)

storeNum = store.getText().toString();
phoneNum = phone.getText().toString();
add = address.getText().toString();
cit = city.getText().toString();
zipCode = zip.getText().toString();
state_picked = myStates.getSelectedItem().toString();

Bundle bundle = new Bundle();
Fragment f = new Fragment();
bundle.putString("store", storeNum);
bundle.putString("phone", phoneNum);
bundle.putString("address", add);
bundle.putString("city", cit);
bundle.putString("zip", zipCode);
bundle.putString("state", state_picked);
f.setArguments(bundle);

我正在像这样替换片段

Fragment fragment = new StoreInfo_Fragment();
                getFragmentManager()
                        .beginTransaction()
                        .setCustomAnimations(android.R.animator.fade_in,
                                android.R.animator.fade_out)
                        .replace(R.id.storeInfo_fragment_container,
                                fragment).commit();

然后在另一个fragment(StoreInfo_Fragment)我这样称呼它

public class StoreInfo_Fragment extends Fragment {
    View view;  

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {        

        view = inflater.inflate(R.layout.fragment_store_info, container,
                false); 

        String mStore = getArguments().getString("store");
        String mPhone = getArguments().getString("phone");
        String mAddress = getArguments().getString("address");
        String mCity = getArguments().getString("city");
        String mZip = getArguments().getString("zip");
        String mState = getArguments().getString("state");  

        TextView store = (TextView) view.findViewById(R.id.store);
        TextView phone = (TextView) view.findViewById(R.id.phone);
        TextView address = (TextView) view.findViewById(R.id.address);
        TextView city = (TextView) view.findViewById(R.id.city);
        TextView zip = (TextView) view.findViewById(R.id.zip);
        TextView state = (TextView) view.findViewById(R.id.state);

        store.setText(mStore);
        phone.setText(mPhone);
        address.setText(mAddress);
        city.setText(mCity);
        zip.setText(mZip);
        state.setText(mState);      

        return view;
    }
}

启动时会给我一个错误,使我的应用程序崩溃。它告诉我这一行有一个空指针异常

String mStore = getArguments().getString("store");

显然,这意味着我没有正确传递我的包,并且所有调用getArguments()都会给 NPE。我哪里做错了?

4

1 回答 1

1

将您的代码更改为:

Bundle bundle = new Bundle();
Fragment fragment = new StoreInfo_Fragment();
bundle.putString("store", storeNum);
bundle.putString("phone", phoneNum);
bundle.putString("address", add);
bundle.putString("city", cit);
bundle.putString("zip", zipCode);
bundle.putString("state", state_picked);
fragment.setArguments(bundle);

// You should use getSupportFragmentManager() here, if you want your app to be backwards compatible
getFragmentManager() 
        .beginTransaction()
        .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
        .replace(R.id.storeInfo_fragment_container, fragment)
        .commit();

您没有为调用事务的片段设置参数。

于 2013-06-05T22:21:35.023 回答