Android:当列表项目编号与列表编号不匹配时,应用程序崩溃

时间:2014-05-17 03:35:02

标签: android android-viewpager

我正在尝试将单个购物清单调整为多个购物清单,然后用户可以从列表列表中选择列表,然后从列表中选择单个产品。

该应用可以使用单个列表正确执行。

从列表中加载产品时,如果产品索引号与列表索引号匹配,则应用程序将加载产品,例如

列表2中的

产品2将加载产品,然后我可以在列表中来回翻页。

尝试加载索引值与列表索引值不匹配的产品,例如列表2中的产品3等会使应用程序崩溃:

05-17 14:46:51.029: E/AndroidRuntime(12888): java.lang.NullPointerException
05-17 14:46:51.029: E/AndroidRuntime(12888):    at com.example.testmylists.ProductFragment.onCreateView(ProductFragment.java:64) 

ProductFragment.java

39      callingFragment = getActivity().getIntent()
40              .getStringExtra("start_product_pager_activity_id");
41      
42      shoppingListId = getActivity().getIntent().getIntExtra(EXTRA_SHOPPING_LIST_ID, 0);
43      
44      if (callingFragment != null) {
45          
46              if (callingFragment.equals("ShoppingListFragment")) {
47              mProduct = MyLists.get(getActivity()).getShoppingList(shoppingListId)
48                          .getProduct(productId); 
49          }
50      }
51  }
52  
53  
54  @Override
55  public View onCreateView(LayoutInflater inflater, ViewGroup parent,
56          Bundle savedInstanceState) {
57      View userView = inflater.inflate(R.layout.fragment_product, parent, false);
58      
59      mProductDetails = (TextView) userView.findViewById(R.id.product_details);
60      String displayInfo = "";
61      
62      if(callingFragment != null) { 
63      
64          if ((mProduct.getAisleNumber() != null) && (mProduct.getBay() != 0) && 
65                  (mProduct.getShelf() != 0)) {

ShoppingListProductPagerActivity.java

27      @TargetApi(11)
28      @Override
29      public void onCreate(Bundle savedInstanceState) {
30          super.onCreate(savedInstanceState);
31          mViewPager = new ViewPager(this);
32      mViewPager.setId(R.id.viewPager);
33         setContentView(mViewPager);
34
35          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
36              getActionBar().setDisplayHomeAsUpEnabled(true);
37         }
38          
39          id =  (UUID)getIntent().getSerializableExtra("shopping_list_id");
40          mShoppingList = MyLists.get(this).getShoppingList(id);
41      
42         FragmentManager fragManager = getSupportFragmentManager();
43      
44          mViewPager.setAdapter(new FragmentStatePagerAdapter(fragManager) {
45
46          @Override           
47             public int getCount() {
48                  return mShoppingList.size();    
49              }
50              @Override
51              public Fragment getItem(int pos) {
52                      UUID productId = mShoppingList.get(pos).getId();
53              return ProductFragment.newInstance(productId);
54              }
55         });

ShoppingListFragment.java

122 @Override
123 public void onListItemClick(ListView lView, View view, int position, long id) {
124     
125     Product mProduct = (Product) (getListAdapter()).getItem(position);
126     Log.d(TAG, "item " + mProduct.getProductDescription());
127     
128     Intent productFragment = new Intent(getActivity(), ShoppingListProductPagerActivity.class);
129     productFragment.putExtra("start_product_pager_activity_id", "ShoppingListFragment");
130     productFragment.putExtra(ProductFragment.EXTRA_SHOPPING_LIST_ID, position);
131     productFragment.putExtra(ProductFragment.EXTRA_PRODUCT_ID, mProduct.getId());
132     productFragment.putExtra("shopping_list_id", items.getShoppingListId());
133     Log.d(TAG, "Item clicked" + mProduct.getProductDescription());
134     
135     startActivity(productFragment);
136 }   

我认为问题存在于ShoppingListProductPagerActivity或ShoppingListFragment的这些代码块中。

MyLists是一个单例类,用于存储购物清单的ArrayList。

1 个答案:

答案 0 :(得分:0)

解决。

productFragment.putExtra(ProductFragment.EXTRA_SHOPPING_LIST_ID, position);
ShoppingListFragment.java中的

替换为:

productFragment.putExtra(ProductFragment.EXTRA_SHOPPING_LIST_UUID, shoppingListId.toString());

在MyLists.java中添加了此方法

    public ShoppingList getShoppingListViaId(UUID id) {
    for (ShoppingList sL : myLists) {
        if (sL.getShoppingListId().equals(id)) {
            return sL;
        }
    }
    return null;
}

在productFragment.java

中更改了此代码
shoppingListId = getActivity().getIntent().getIntExtra(EXTRA_SHOPPING_LIST_ID, 0);

if (callingFragment != null) {

if (callingFragment.equals("ShoppingListFragment")) {
mProduct = MyLists.get(getActivity()).getShoppingList(shoppingListId)
.getProduct(productId); 
 }
}

为:

        shoppingListId = UUID.fromString(getActivity().getIntent()
                    .getStringExtra(EXTRA_SHOPPING_LIST_UUID));

    if (callingFragment != null) {

        if (callingFragment.equals("ShoppingListFragment")) {
            mProduct = MyLists.get(getActivity()).getShoppingListViaId(shoppingListId)
                    .getProduct(productId); 
        }
    }

简而言之,当我应该传递一个UUID时,试图传递一个int来识别购物清单。