2 developerwab developerWab 于 2013.06.06 13:46 提问

安卓中传递得到空指针

在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 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");

有空指针。

很显然是传递时出现错误了。

请高手指教。

1个回答

cj6585256
cj6585256   2013.06.06 14:17

应该是第二段代码
下面是伪代码
Fragment fragment = new StoreInfo_Fragment();
fragment.setAruguments(Bundle yourBundle);
FragmentMannager.replace().commit();

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!