0

我有一个类,我通过标准的 get/set 方法分配字符串值。设置值后,它按预期返回它们。当我在另一个片段中调用类值时,它们返回 null。我似乎无法弄清楚。

我如何设置课程并设置值。

public class BasicInfo_Assets_Fragment extends Fragment {
    View view
    public static Store_Model store_model = null;
    ...

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

    view = inflater.inflate(R.layout.fragment_basic_info, container, false);
    ...
    zipCode = zip.getText().toString();        
    // set info        
    store_model = new Store_Model();
    store_model.setZip(zipCode);
    Log.v("STORE" , "Zip: " + store_model.getZip());
    ...

Log.v 完全按照预期打印出:“Zip: 47564”。但是,当我像这样在另一个片段中调用该类时,它会给我一个错误。

public class Options_ListFragment extends ListFragment {
    View view;
    public static Store_Model store_model = BasicInfo_Assets_Fragment.store_model;
    ....
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_options, container, false);
    Log.v("STORE", "Zip: " + store_model.getZip()); <!--- ERROR

如代码中所述,尝试提取该值会给我一个空指针异常。我不明白这个类是如何失去它的价值的。也许我只需要另一双眼睛。我在这里先向您的帮助表示感谢

编辑

谢谢@Juan Jose Fidalgo 和@SJuan76。我想通了,我在 Options_ListFragment 中将代码更改为以下内容:

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

        view = inflater.inflate(R.layout.fragment_options, container, false);       
        if (BasicInfo_Assets_Fragment.store_model != null) {
            LinearLayout ll = (LinearLayout) view
                    .findViewById(R.id.assets_store_info);
            ll.setVisibility(View.VISIBLE);

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

            getStoreInfo();
        }

public void getStoreInfo() {
        String mStore = BasicInfo_Assets_Fragment.store_model.getStoreNum();
        String mPhone = BasicInfo_Assets_Fragment.store_model.getPhoneNum();
        String mAddress = BasicInfo_Assets_Fragment.store_model.getAddress();
        String mCity = BasicInfo_Assets_Fragment.store_model.getCity();
        String mZip = BasicInfo_Assets_Fragment.store_model.getZip();
        String mState = BasicInfo_Assets_Fragment.store_model.getState();

        store.setText("Store #: " + mStore);
        phone.setText("Phone #: " + mPhone);
        address.setText("Address: " + mAddress);
        city.setText("City: " + mCity);
        zip.setText("Zip: " + mZip);
        state.setText("State: " + mState);
    }
4

2 回答 2

1

您正在创建两个变量。一个作为 in 中BasicInfo_Assets_Fragment的属性,另一个作为 in 中的属性Option_ListFragment。它们是完全不同的变量。

如果您在 中引用了BasicInfo_Assets_Fragment实例(让我们称之为biafOption_ListFragment,您可以将其视图属性引用为biaf.storeModel,这将给出您想要的。

最佳实践是storeModel私有化getStoreModel()并向BasicInfo_Assets_Fragment. 此外,请尝试遵循 Java 命名约定。

于 2013-06-12T18:49:46.507 回答
1

片段

Options_ListFragment
   public static Store_Model store_model = BasicInfo_Assets_Fragment.store_model;

在比之前执行

BasicInfo_Assets_Fragment.onCreateView(...)

为此,当,

public static Store_Model store_model = BasicInfo_Assets_Fragment.store_model;

被执行,变量

BasicInfo_Assets_Fragment.store_model 

包含空值。所以,

Options_ListFragment
  public static Store_Model store_model = BasicInfo_Assets_Fragment.store_model;

得到一个空值。

片段

 Options_ListFragment
   public static Store_Model store_model = BasicInfo_Assets_Fragment.store_model;

在 ClassLoader 第一次在代码的任何位置调用 Options_ListFragment 类时执行 Options_ListFragment 类。您应该查看何时调用 Options_ListFragment。

于 2013-06-12T18:55:43.750 回答