CoverFlow不显示图像

时间:2016-11-09 06:35:02

标签: android android-layout baseadapter coverflow

我正在使用coverflow来显示我的图片,但是当我运行我的项目时它会崩溃并发出以下错误..任何人都可以帮我解决这个问题吗?以下是我的代码段代码,任何帮助都会被评估

  

:致命的例外:主要                                                                                 处理:com.example.daysheft.dpc,PID:21087                                                                                 java.lang.NullPointerException:尝试调用接口方法' int android.widget.Adapter.getCount()'在null对象引用上                                                                                     at it.moondroid.coverflow.components.ui.containers.FeatureCoverFlow.dispatchDraw(FeatureCoverFlow.java:471)                                                                                     在android.view.View.updateDisplayListIfDirty(View.java:15185)                                                                                     在android.view.View.draw(View.java:15964)

CoverflowActivity.java

public class CoverFlowActivity extends ActionBarActivity {

    private FeatureCoverFlow mCoverFlow;
    private CoverFlowAdapter mAdapter;
  //  private ArrayList<GameEntity> mData = new ArrayList<>(0);
    private TextSwitcher mTitle;
    private String productlanding_url;
    private String ProductID;

    private ProgressDialog pDialog;
    private ArrayList<PackListModel> categorylist;
    private ArrayList<PackSizeModel> subcat_list;
    private PackListModel cm;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coverflow);

         ProductID=getIntent().getStringExtra("Product_Id");

        productlanding_url=Globaldata.APP_URL+"ProductLanding";
        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

        GetProductLanding();

       /* mData.add(new GameEntity(R.drawable.tesss, R.string.title1));
        mData.add(new GameEntity(R.drawable.tesss, R.string.title2));
        mData.add(new GameEntity(R.drawable.tesss, R.string.title3));
        mData.add(new GameEntity(R.drawable.tesss, R.string.title4));*/

       /* mTitle = (TextSwitcher) findViewById(R.id.title);
        mTitle.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                LayoutInflater inflater = LayoutInflater.from(CoverFlowActivity.this);
                TextView textView = (TextView) inflater.inflate(R.layout.item_title, null);
                return textView;
            }
        });
        Animation in = AnimationUtils.loadAnimation(this, R.anim.slide_in_top);
        Animation out = AnimationUtils.loadAnimation(this, R.anim.slide_out_bottom);
        mTitle.setInAnimation(in);
        mTitle.setOutAnimation(out);
*/
        /*mAdapter = new CoverFlowAdapter(CoverFlowActivity.this,categorylist);
        mAdapter.setData(categorylist);*/
        mCoverFlow = (FeatureCoverFlow) findViewById(R.id.coverflow);
     //   mCoverFlow.setAdapter(mAdapter);

        mCoverFlow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              /*  Toast.makeText(CoverFlowActivity.this,
                        getResources().getString(mData.get(position).titleResId),
                        Toast.LENGTH_SHORT).show();*/
            }
        });

        mCoverFlow.setOnScrollPositionListener(new FeatureCoverFlow.OnScrollPositionListener() {
            @Override
            public void onScrolledToPosition(int position) {
               // mTitle.setText(getResources().getString(mData.get(position).titleResId));
            }

            @Override
            public void onScrolling() {
                //mTitle.setText("");
            }
        });

    }

    public void GetProductLanding()
    {

        showpDialog();

        Map<String, String> params = new HashMap<String, String>();
        params.put("productId", "1004");
        params.put("userID", "1");

        System.out.println("PARAMS ProductLanding" + params);
        JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST, productlanding_url, new JSONObject(params),
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        System.out.println("response of ProductLanding" + response.toString());

                       try {

                            for (int i = 0; i < response.length(); i++) {

                                JSONObject person = (JSONObject) response
                                        .get(i);
                                JSONArray itemlist=person.optJSONArray("itemList");
                                System.out.println("ProductLanding itemlist"+itemlist);

                               for(int j=0;j<itemlist.length();j++)
                                {
                                    JSONObject itmobj = (JSONObject)itemlist
                                            .get(j);


                                    JSONArray typemasters=itmobj.optJSONArray("packInfo");

                                   categorylist=new ArrayList<PackListModel>();

                                    for(int k=0;k<typemasters.length();k++)
                                    {

                                        subcat_list = new ArrayList<PackSizeModel>();
                                        JSONObject typemasterobj=(JSONObject)typemasters.get(k);
                                        cm=new PackListModel();
                                        cm.setProductLanding_packId(typemasterobj.getString("packId"));
                                        cm.setProductLanding_packLink(typemasterobj.getString("packDesc"));
                                        cm.setProductLanding_packDesc(typemasterobj.getString("packLink"));


                                        if (typemasterobj.getString("packSizeDetails").equals("null")) {
                                            System.out.println("arr null");
                                        } else {
                                            JSONArray SubcategoryArr = typemasterobj.optJSONArray("packSizeDetails");

                                            for (int l = 0; l < SubcategoryArr.length(); l++) {

                                                JSONObject Subcategoryobj = SubcategoryArr.getJSONObject(l);
                                                String packQty = Subcategoryobj.getString("packQty");
                                                String packSize = Subcategoryobj.getString("packSize");
                                                String sellingPrice = Subcategoryobj.getString("sellingPrice");
                                                String lineThroughPrice = Subcategoryobj.getString("lineThroughPrice");
                                                String packSizePrice = Subcategoryobj.getString("packSizePrice");
                                                String freeShipping = Subcategoryobj.getString("freeShipping");
                                                String reOrderPrice = Subcategoryobj.getString("reOrderPrice");
                                                PackSizeModel ch = new PackSizeModel();
                                                ch.setPackSize_packQty(packQty);
                                                ch.setPackSize_packSize(packSize);
                                                ch.setPackSize_sellingPrice(sellingPrice);
                                                ch.setPackSize_lineThroughPrice(lineThroughPrice);
                                                ch.setPackSize_packSizePrice(packSizePrice);
                                                ch.setPackSize_freeShipping(freeShipping);
                                                ch.setPackSize_reOrderPrice(reOrderPrice);
                                                // ch.setChild_Id(jo.getString("categoryID"));
                                                subcat_list.add(ch);
                                            } // for loop end

                                            cm.setItems(subcat_list);


                                        }

                                        categorylist.add(cm);

                                    }

                                    System.out.println("SIZE"+categorylist.size());
                                   /* mAdapter = new CoverFlowAdapter(CoverFlowActivity.this,categorylist);
                                    mAdapter.setData(categorylist);
                                    mCoverFlow.setAdapter(mAdapter);*/
                                }

                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                            System.out.println("Error" + e.getMessage());
                            // Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }

                        hidepDialog();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.println("change Pass response -->> " + error.toString());
                        hidepDialog();
                    }
                });

        request.setRetryPolicy(new

                DefaultRetryPolicy(60000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));


        Volley.newRequestQueue(CoverFlowActivity.this).add(request);
    }


    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }


}

1 个答案:

答案 0 :(得分:0)

Your categorylist must have value before you set it to mAdapter. To overcome from this error initialize your mAdapter in response of your volley request. For better solution use this library if it is suitable for your application.