Android应用程序因回收站视图崩溃

时间:2017-04-30 10:06:32

标签: android crash android-recyclerview

我搜索了所有网络,但没有找到任何解决方案。我从在线数据库中获取数据并在回收站视图中显示但不幸的是应用程序车库。我不知道为什么。我附加了所有logcat,我的相关代码也附加了。任何帮助将不胜感激。

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.liger.admin_panel, PID: 28935
                  java.lang.NullPointerException
                      at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6368)
                      at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5555)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
                      at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)
                      at android.view.View.measure(View.java:16539)
                      at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
                      at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
                      at android.view.View.measure(View.java:16539)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                      at android.view.View.measure(View.java:16539)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
                      at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
                      at android.view.View.measure(View.java:16539)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                      at android.view.View.measure(View.java:16539)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1406)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
                      at android.view.View.measure(View.java:16539)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5140)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                      at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2421)
                      at android.view.View.measure(View.java:16539)
                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1959)
                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1155)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1337)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1042)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                      at android.view.Choreographer.doFrame(Choreographer.java:544)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                      at android.os.Handler.handleCallback(Handler.java:733)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:5103)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
                      at dalvik.system.NativeStart.main(Native Method)

RecyclerView类代码

package com.example.liger.admin_panel;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class recycler_view extends AppCompatActivity{

    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
//    RecyclerView.LayoutManager layoutManager;

    ArrayList<items>item;
    RequestQueue requestQueue;
    //int requestCount = 1;

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


        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setHasFixedSize(true);

        item = new ArrayList<items>();
        requestQueue = Volley.newRequestQueue(this);

        getData();

      //  recyclerView.setOnScrollChangeListener(this);

        adapter = new itemsAdapter(item, getApplicationContext());
        recyclerView.setAdapter(adapter);
    }

    private void getData() {
        requestQueue.add(getDataFromServer());
       // requestCount++;
    }

    private JsonArrayRequest getDataFromServer() {
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        progressBar.setVisibility(View.VISIBLE);
        progressBar.setIndeterminate(true);

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(constants.get_items,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        parseData(response);
                        progressBar.setVisibility(View.GONE);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        progressBar.setVisibility(View.GONE);
                        Toast.makeText(getApplicationContext(), "No More Items Available", Toast.LENGTH_SHORT).show();
                    }
                });
        return jsonArrayRequest;
    }

    private void parseData(JSONArray array){
        for (int i = 0; i < array.length(); i++){
            JSONObject json= null;
            try {

                json = array.getJSONObject(i);
                items obj = new items(json.getString("item_name"),
                                      json.getString("item_category"),
                                      json.getString("item_price"),
                                      json.getString("item_rating"),
                                      constants.img_url+json.getString("item_img"));

                item.add(obj);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        adapter.notifyDataSetChanged();
    }

    private boolean isLastItemDisplaying(RecyclerView recyclerView){
        if (recyclerView.getAdapter().getItemCount() != 0) {
            int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
            if (lastVisibleItemPosition != recyclerView.NO_POSITION &&lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
                return true;
        }
        return false;
    }

}

ItemsAdapter类代码

package com.example.liger.admin_panel;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

/**
 * Created by liger on 4/30/2017.
 */

public class itemsAdapter extends android.support.v7.widget.RecyclerView.Adapter<itemsAdapter.ItemViewHolder> {

    ArrayList<items>item = new ArrayList<items>();
    Context context;

    public itemsAdapter(ArrayList<items> item, Context ctx) {
        this.item = item;
        this.context = ctx;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_layout,parent,false);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);

        return null;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        items obj = item.get(position);

        Glide.with(this.context).load(obj.getImg_name()).into(holder.item_img);
        holder.item_name.setText(obj.getName());
        holder.item_category.setText(obj.getCategory());
        holder.item_price.setText(obj.getPrice());
        holder.item_rating.setRating(Float.parseFloat(obj.getRating()));
    }

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

    public static class ItemViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder{

        ImageView item_img;
        TextView item_name,item_price,item_category;
        RatingBar item_rating;

        public ItemViewHolder(View itemView)
            super(itemView);

            item_img = (ImageView) itemView.findViewById(R.id.card_img);
            item_name = (TextView) itemView.findViewById(R.id.card_name);
            item_price = (TextView) itemView.findViewById(R.id.card_price);
            item_category= (TextView) itemView.findViewById(R.id.card_category);
            item_rating = (RatingBar) itemView.findViewById(R.id.card_rating);
        }
    }
}

0 个答案:

没有答案