我在一个活动中集成了两个片段。一个片段具有可扩展列表视图,另一个片段具有列表中所选项目的详细信息。据我所知,没有活动,我们无法将值从一个活动传递到另一个活动。所以我在列表片段中创建了Interface并添加了以下代码。
OnItemSelectedListener mCallback;
public interface OnItemSelectedListener {
public void onItemSelected(int menuID, String sShortDesc, String sDescription, double dRate);
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
mCallback = (OnItemSelectedListener) activity;
}
并且在项目选择的事件中,我添加了以下代码。
mCallback.onItemSelected(oALMenu.get(groupPosition).getCategoryMenuItem().get(childPosition).getMenuID() , oALMenu.get(groupPosition).getCategoryMenuItem().get(childPosition).getItemShortDesc(),oALMenu.get(groupPosition).getCategoryMenuItem().get(childPosition).getDescription(), oALMenu.get(groupPosition).getCategoryMenuItem().get(childPosition).getRate());
现在,我已经在我的主要活动中实现了接口,并实现了如下方法。
@Override
public void onItemSelected(int menuID, String sShortDesc, String sDescription, double dRate) {
// TODO Auto-generated method stub
Detail_Fragment oDetail_Fragment = (Detail_Fragment) getFragmentManager().findFragmentById(R.id.fragment_Details);
System.out.println (">> " + oDetail_Fragment);
System.out.println(">> " + menuID + " >> " + sShortDesc);
if(oDetail_Fragment != null)
oDetail_Fragment.setValue(menuID, sShortDesc, sDescription, dRate);
}
根据System.out语句,我获得此级别的值,但无法更新详细信息片段上的组件。
详细碎片代码如下。
public class Detail_Fragment extends Fragment {
TextView oTVShortDesc, oTVDescription, oTVRate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
System.out.println("Value : onCreateView");
Bundle oBundle = getArguments();
View oView = inflater.inflate(R.layout.layout_detail_fragment, null,false);
oTVShortDesc = (TextView) oView.findViewById(R.id.tvItemShortDesc);
oTVDescription = (TextView) oView.findViewById(R.id.tvDescription);
oTVRate = (TextView) oView.findViewById(R.id.tvRate);
return oView;
}
public void setValue(int iMenuID, String sShortDesc, String sDescription, double dRate){
System.out.println("Value : " + oTVShortDesc);
TextView oTVShortDesc = (TextView) getView().findViewById(R.id.tvItemShortDesc);
System.out.println("Value : " + oTVShortDesc);
TextView oTVDescription = (TextView) getView().findViewById(R.id.tvDescription);
TextView oTVRate = (TextView) getView().findViewById(R.id.tvRate);
oTVShortDesc.setText(sShortDesc);
oTVShortDesc.setTag(iMenuID);
oTVDescription.setText(sDescription);
oTVRate.setText(String.valueOf(dRate));
}
}
在上面运行代码时遇到以下错误。
02-07 17:09:59.938: E/AndroidRuntime(25817): FATAL EXCEPTION: main
02-07 17:09:59.938: E/AndroidRuntime(25817): Process: com.agilissystems.restaurantapp, PID: 25817
02-07 17:09:59.938: E/AndroidRuntime(25817): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
02-07 17:09:59.938: E/AndroidRuntime(25817): at com.agilissystems.restaurantapp.Detail_Fragment.setValue(Detail_Fragment.java:38)
02-07 17:09:59.938: E/AndroidRuntime(25817): at com.agilissystems.restaurantapp.MenuActivity.onItemSelected(MenuActivity.java:139)
02-07 17:09:59.938: E/AndroidRuntime(25817): at com.agilissystems.restaurantapp.List_Fragment$MenuExpandableListViewAdapter.getChildView(List_Fragment.java:278)
02-07 17:09:59.938: E/AndroidRuntime(25817): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)
02-07 17:09:59.938: E/AndroidRuntime(25817): at android.widget.AbsListView.obtainView(AbsListView.java:2344)
02-07 17:09:59.938: E/AndroidRuntime(25817): at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
02-07 17:09:59.938: E/AndroidRuntime(25817): at android.widget.ListView.onMeasure(ListView.java:1182)
02-07 17:09:59.938: E/AndroidRuntime(25817): at android.view.View.measure(View.java:17430)
02-07 17:09:59.938: E/AndroidRuntime(25817): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
请告知。