developerWab 2013-06-06 05:46 采纳率: 0%
浏览 3065

安卓中传递得到空指针

在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条回答 默认 最新

  • 码密码密哄 2013-06-06 06:17
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝