如何调试应用程序在获取JSON数据时崩溃的原因?

时间:2017-07-05 06:12:40

标签: java android json

我的应用程序崩溃了。我使用卡查看卡视图使用自定义列表视图。

我的JSON:

{"College":[{"Name":"NITK","Logo":"http:\/\/192.168.43.164\/Webapp\/Collegelogos\/nitk.jpg"},{"Name":"IITR","Logo":"http:\/\/192.168.43.164\/Webapp\/Collegelogos\/iitr.jpg"},{"Name":"NITT","Logo":"http:\/\/192.168.43.164\/Webapp\/Collegelogos\/nitt.jpg"},{"Name":"IITB","Logo":"http:\/\/192.168.43.164\/Webapp\/Collegelogos\/iitb.png"},{"Name":"IITG","Logo":"http:\/\/192.168.43.164\/Webapp\/Collegelogos\/iitg.png"},{"Name":"IITD","Logo":"http:\/\/192.168.43.164\/Webapp\/Collegelogos\/iitd.png"},{"Name":"BITS","Logo":"http:\/\/192.168.43.164\/Webapp\/Collegelogos\/bits.png"}]}

我的代码:

public class MainActivity extends AppCompatActivity {
    private List<College> listcollege;

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

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

        //Initializing Views
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        listcollege = new ArrayList<>();


        adapter = new CardAdapter(this, listcollege);
        recyclerView.setAdapter(adapter);

        getData();

    }

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

        //Creating a json array request
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,Config.DATA_URL,null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        //Dismissing progress dialog
                        Toast.makeText(MainActivity.this, "sachin", Toast.LENGTH_SHORT).show();
                        loading.dismiss();

                        try {
                            // College
                            JSONArray jsonArrayCollege = response.getJSONArray("College");

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

                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonObjectRequest);
    }
    public  void parseData(JSONArray array) {
        for (int i = 0; i < array.length(); i++) {
            College college = new College();
            JSONObject json = null;
            try {
                json = array.getJSONObject(i);
                college.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
                String s = (json.getString(Config.TAG_NAME));
                Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
                college.setName(s);

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

}

        RequestQueue requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonObjectRequest);
    }
    public  void parseData(JSONArray array) {
        for (int i = 0; i < array.length(); i++) {
            College college = new College();
            JSONObject json = null;
            try {
                json = array.getJSONObject(i);
                college.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
                String s = (json.getString(Config.TAG_NAME));
                Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
                college.setName(s);

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

错误,Android监视器:

  

致命的例外:主要                                                                                   处理:com.example.sachin.volleyrecycle,PID:6825                                                                                   java.lang.NullPointerException:尝试调用虚方法&#39; int   java.lang.String.hashCode()&#39;在null对象引用上                                                                                       在com.android.volley.Request。(Request.java:136)                                                                                       在   com.android.volley.toolbox.ImageRequest。(ImageRequest.java:71)                                                                                       在com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:219)                                                                                       在com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:171)                                                                                       在   com.example.sachin.volleyrecycle.CardAdapter.onBindViewHolder(CardAdapter.java:48)                                                                                       在   com.example.sachin.volleyrecycle.CardAdapter.onBindViewHolder(CardAdapter.java:24)                                                                                       在   android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6356)                                                                                       在   android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6389)                                                                                       在   android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)                                                                                       在   android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)                                                                                       在   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5440)                                                                                       在   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5436)                                                                                       在   android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2224)                                                                                       在   android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)                                                                                       在   android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)                                                                                       在   android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)                                                                                       在   android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)                                                                                       在   android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)                                                                                       在android.view.View.measure(View.java:19857)                                                                                       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)                                                                                       在   android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)                                                                                       在android.widget.LinearLayout.measureVertical(LinearLayout.java:758)                                                                                       在android.widget.LinearLayout.onMeasure(LinearLayout.java:640)                                                                                       在android.view.View.measure(View.java:19857)                                                                                       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)                                                                                       在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                                       在   android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)                                                                                       在android.view.View.measure(View.java:19857)                                                                                       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)                                                                                       在   android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)                                                                                       在android.view.View.measure(View.java:19857)                                                                                       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)                                                                                       在android.widget.FrameLayout.onMeasure(FrameLayout.java:185)                                                                                       在android.view.View.measure(View.java:19857)                                                                                       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)                                                                                       在   android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)                                                                                       在android.widget.LinearLayout.measureVertical(LinearLayout.java:758)                                                                                       在android.widget.LinearLayout.onMeasure(LinearLayout.java:640)                                                                                       在android.view.View.measure(View.java:19857)                                                                                       在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)

0 个答案:

没有答案