传递包时获取空指针

时间:2013-06-05 22:08:02

标签: android android-fragments

所以我将这段代码放在一个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。我哪里出错了?

1 个答案:

答案 0 :(得分: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();

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