凌空单身中的空指针异常

我已经从developer.android.com复制了volleySingleton代码,并尝试从回收站视图中的URL中获取图像,但是在加载数据应用程序崩溃时。 我尝试了一些修补程序,但他们没有工作

这里是从developer.android.com复制的volleySingleton类

public class MyApplication extends Application {
    private static MyApplication mInstance;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static Context mCtx;

    private MyApplication(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();

        mImageLoader = new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap>
                            cache = new LruCache<String, Bitmap>(20);

                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                });
    }

    public static synchronized MyApplication getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MyApplication(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

    public ImageLoader getImageLoader() {
        return mImageLoader;
    }
}

这是logcat报告

显示java.lang.NullPointerException
在com.aninesoft.meltwishbeta.MyApplication.getRequestQueue(MyApplication.java:61)
在com.aninesoft.meltwishbeta.MyApplication。(MyApplication.java:31)
在com.aninesoft.meltwishbeta.MyApplication.getInstance(MyApplication.java:52)
在com.aninesoft.meltwishbeta.RecyclerViewDataAdapter.onBindViewHolder(RecyclerViewDataAdapter.java:45)
在com.aninesoft.meltwishbeta.RecyclerViewDataAdapter.onBindViewHolder(RecyclerViewDataAdapter.java:19)
在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:5277)
在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:5310)
在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4568)
在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4461)
在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:1962)
在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
在android.view.View.layout(View.java:14832)
在android.view.ViewGroup.layout(ViewGroup.java:4640)
在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
在android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
在android.view.View.layout(View.java:14832)
在android.view.ViewGroup.layout(ViewGroup.java:4640)
在android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
在android.view.View.layout(View.java:14832)
在android.view.ViewGroup.layout(ViewGroup.java:4640)
在android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1034)
在android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:744)
在android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
在android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1180)
在android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:757)
在android.view.View.layout(View.java:14832)
在android.view.ViewGroup.layout(ViewGroup.java:4640)
在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
在android.widget.FrameLayout.onLayout(FrameLayout.java:388)
在android.view.View.layout(View.java:14832)
在android.view.ViewGroup.layout(ViewGroup.java:4640)
在android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
在android.view.View.layout(View.java:14832)
在android.view.ViewGroup.layout(ViewGroup.java:4640)
在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
在android.widget.FrameLayout.onLayout(FrameLayout.java:388)
在android.view.View.layout(View.java:14832)
在android.view.ViewGroup.layout(ViewGroup.java:4640)
在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
在android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
在android.view.View.layout(View.java:14832)
在android.view.ViewGroup.layout(ViewGroup.java:4640)
在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
在android.widget.FrameLayout.onLayout(FrameLayout.java:388)
在android.view.View.layout(View.java:14832)
在android.view.ViewGroup.layout(ViewGroup.java:4640)
在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1996)
在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1753)
在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
在android.view.ViewRootImpl $

这是适配器类

public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ViewHolder> {
    private Context context;
    public ImageLoader mImageLoader;

    //List of superHeroes
    List<ListOfData> superHeroes;

    public RecyclerViewDataAdapter(List<ListOfData> superHeroes, Context context){
        super();
        //Getting all the superheroes
        this.superHeroes = superHeroes;
        this.context=context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview_row, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ListOfData superHero =  superHeroes.get(position);

        mImageLoader = MyApplication.getInstance(context).getImageLoader();
        mImageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView1, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

        holder.imageView1.setImageUrl(superHero.getImageUrl(), mImageLoader);
        holder.textViewName1.setText(superHero.getName());
        //holder.textViewRank.setText(String.valueOf(superHero.getRank()));
        //holder.textViewRealName.setText(superHero.getRealName());
        //holder.textViewCreatedBy.setText(superHero.getCreatedBy());
        //holder.textViewFirstAppearance.setText(superHero.getFirstAppearance());

        String powers = "";

       /* for(int i = 0; i<superHero.getPowers().size(); i++){
            powers+= superHero.getPowers().get(i);
        }*/

        //holder.textViewPowers.setText(powers);

    }

    @Override
    public int getItemCount() {
        return superHeroes.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        public NetworkImageView imageView1;
        public NetworkImageView imageView2;
        public TextView textViewName1;
        public TextView textViewName2;
        public TextView textViewRank;
        public TextView textViewRealName;
        public TextView textViewCreatedBy;
        public TextView textViewFirstAppearance;
        public TextView textViewPowers;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView1 = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
            //imageView2 = (NetworkImageView) itemView.findViewById(R.id.thumbnail2);
           textViewName1 = (TextView) itemView.findViewById(R.id.textViewName);//textViewName2 = (TextView) itemView.findViewById(R.id.category_title_two);
            /* textViewRank= (TextView) itemView.findViewById(R.id.textViewRank);
            textViewRealName= (TextView) itemView.findViewById(R.id.textViewRealName);
            textViewCreatedBy= (TextView) itemView.findViewById(R.id.textViewCreatedBy);
            textViewFirstAppearance= (TextView) itemView.findViewById(R.id.textViewFirstAppearance);
            textViewPowers= (TextView) itemView.findViewById(R.id.textViewPowers);
            */
        }
    }
}

and Json part

    public class Pants extends Fragment {

    //Creating a List of superheroes

    public Context context;
    public List<ListOfData> listSuperHeroes;

    //Creating Views
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter adapter;


    public Pants() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.tshirts_pants_shirts, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);

        //Calling method to get data
        getData();
        return view;
    }


    private void getData() {
        //Showing a progress dialog
        final ProgressDialog loading = ProgressDialog.show(getActivity(), "Loading Data", "Please wait...", false, false);

        //Creating a json array request
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ProductConfig.DATA_URL,

                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        //Dismissing progress dialog
                        loading.dismiss();

                        //calling method to parse json array
                        parseData(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

        //Adding request to the queue
        requestQueue.add(jsonArrayRequest);
    }

    //This method will parse json data
    private void parseData(JSONArray array) {
        listSuperHeroes = new ArrayList<>();
        for (int i = 0; i < array.length(); i++) {
            ListOfData superHero = new ListOfData();
            JSONObject json = null;
            try {
                json = array.getJSONObject(i);
                superHero.setImageUrl(json.getString(ProductConfig.TAG_IMAGE_URL));
                superHero.setName(json.getString(ProductConfig.TAG_NAME));
                /*superHero.setRank(json.getInt(ProductConfig.TAG_RANK));
                superHero.setRealName(json.getString(ProductConfig.TAG_REAL_NAME));
                superHero.setCreatedBy(json.getString(ProductConfig.TAG_CREATED_BY));
                superHero.setFirstAppearance(json.getString(ProductConfig.TAG_FIRST_APPEARANCE));
                */

                ArrayList<String> powers = new ArrayList<String>();

                JSONArray jsonArray = json.getJSONArray(ProductConfig.TAG_POWERS);

                for (int j = 0; j < jsonArray.length(); j++) {
                    powers.add(((String) jsonArray.get(j)) + "n");
                }
                superHero.setPowers(powers);


            } catch (JSONException e) {
                e.printStackTrace();
            }
            listSuperHeroes.add(superHero);
        }
        adapter = new RecyclerViewDataAdapter(listSuperHeroes, context);

        //Adding adapter to recyclerview
        recyclerView.setAdapter(adapter);
        //Initializing our superheroes list



    }
}
链接地址: http://www.djcxy.com/p/93157.html

上一篇: Null pointer exception in volley singleton

下一篇: Set default selection for a spinner