我有一个类,我通过标准的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);
}
答案 0 :(得分:1)
您正在创建两个变量。一个作为BasicInfo_Assets_Fragment
中的属性,另一个作为Option_ListFragment
中的属性。它们是完全不同的变量。
如果您在BasicInfo_Assets_Fragment
中引用了biaf
实例(我们称之为Option_ListFragment
),您可以将其视图属性引用为biaf.storeModel
,这将是给你想要的东西。
最佳做法是将storeModel
设为私有,并向getStoreModel()
添加BasicInfo_Assets_Fragment
方法。另外,请尝试遵循Java命名约定。
答案 1 :(得分: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;
第一次在代码的任何位置调用类Options_ListFragment时ClassLoader加载类Options_ListFragment时执行。您应该在调用Options_ListFragment时查看。