这是我们将调用FragmentClass方法的片段的父活动。片段类的引用显示空指针异常。
下面我们将看到从父活动开始的片段。
public class HomeActivity extends FragmentActivity {
RocketMandiModel modelObject = new RocketMandiModel();
ViewPager viewPager = null;
ViewFlipper homeActivityViewFlipper;
ImageView homeImageView;
ImageView shoppingCartImageView;
ImageView accountImageView;
ListView listView ;
Fragment fragmentFruits;
Fragment fragmentVegetables;
private TextView infoTextViewInShoppingCart;
private TextView checkOutTextViewInShoppingCart;
public List<OrderItemDetails> ordersList = new ArrayList<OrderItemDetails>();
ShoppingCartAdapter adp;
int vegetableID;
View v;
//private String username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// check if someone is logged in or not
setContentView(R.layout.activity_home);
modelObject.initializeLists();
setFlipper();
setHomeImageView();
setShoppingCartImageView();
setAccountImageView();
setTextViews();
listView = (ListView) findViewById(R.id.products_orders_list_view_at_shopping_cart);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
}
class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return the Fragment associated with a specified position.
*
* @param position
*/
@Override
public Fragment getItem(int position) {
if(position == 0){
fragmentVegetables = new FragmentVegetables();
Bundle args = new Bundle();
args.putSerializable("key", modelObject);
fragmentVegetables.setArguments(args);
vegetableID = fragmentVegetables.getId();
return fragmentVegetables;
}
if(position==1){
fragmentFruits = new FragmentFruits();
Bundle args = new Bundle();
args.putSerializable("key", modelObject);
fragmentFruits.setArguments(args);
return fragmentFruits;
}
return null;
}
}
}
现在我们调用片段类方法的方法。 缺少一些与此问题无关的代码。
private void deleteProduct(int position){
FragmentManager manager = fragmentVegetables.getFragmentManager();
FragmentVegetables fragment = (FragmentVegetables) manager.findFragmentById(vegetableID);
fragment.setVegetableListItemZero(0);
}
我也试过,manage = getSupportManager();并且没有fragmentVegetable.getM ....对象 但fragment.setVegetableListItemZero()始终显示空指针。 那片段没有任何参考。
现在我将展示Fragment活动和名为。
的方法public class FragmentVegetables extends Fragment {
private List<Vegetables> vegetablesList = new ArrayList<Vegetables>();
private View v;
private RocketMandiModel modelObject;
List<Integer> vegetableQuantityList;
public View onCreateView(LayoutInflater inflater, ViewGroup container , Bundle saveInstanceState) {
/**
* I do not know how inflator and shit works, I learned it from you tube and it
* just work.
*/
v = inflater.inflate(R.layout.fragment_vegetables, container, false);
setModelObject();
populateVegetableList();
populateVegetableListView();
return v;
}
}
}
还有一些其他代码与问题无关,并且工作正常。
public void setVegetableListItemZero(int positionOfDeletedProduct){
Toast.makeText(getActivity().getApplicationContext(),"This is working :" , positionOfDeletedProduct);
}
这是log cat,其中发生空指针异常。
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.thefuelcompany.rocketmandi.HomeActivity.deleteProduct(HomeActivity.java:291)
at com.thefuelcompany.rocketmandi.HomeActivity.access$200(HomeActivity.java:27)
at com.thefuelcompany.rocketmandi.HomeActivity$ShoppingCartAdapter$1.onClick(HomeActivity.java:254)
at android.view.View.performClick(View.java:4293)
at android.view.View$PerformClick.run(View.java:17535)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$
MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
更改您的deleteProductmethod代码
private void deleteProduct(int position){
if(fragmentVegetables!=null){
fragmentVegetables.setVegetableListItemZero(0);
}
}