不幸的是,使用JSON

时间:2017-10-21 08:08:23

标签: android mysql sql json android-recyclerview

  

致命的例外:主要1

     

0-21 15:37:18.680 20525-20525 / info.androidhive.loginandregistration   E / AndroidRuntime:进程:info.androidhive.loginandregistration,PID:   20525 10-21 15:37:18.680   20525-20525 / info.androidhive.loginandregistration E / AndroidRuntime:   java.lang.NullPointerException:尝试调用虚方法'void   android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)'上   null object reference 10-21 15:37:18.680   20525-20525 / info.androidhive.loginandregistration E / AndroidRuntime:
  在   info.androidhive.loginandregistration.activity.ByobovateFragment.onCreateView(ByobovateFragment.java:92)   10-21 15:37:18.680 20525-20525 / info.androidhive.loginandregistration   E / AndroidRuntime:at   android.support.v4.app.Fragment.performCreateView(Fragment.java:1965)

这是我的错误 我正在使用碎片,我不知道什么是错的。请回答。谢谢。 :)

我的片段代码

public class ByobovateFragment extends Fragment {

private static final String URL_DATA = "http://192.168.8.7/android_login_api/leafshape.php";

private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;

private List<ListItem> listItems;

private void loadRecycleViewData() {
    final ProgressDialog progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Loading data...");
    progressDialog.show();

    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_DATA, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            progressDialog.dismiss();
            try {
                JSONObject jsonObject = new JSONObject(s);
                JSONArray array = jsonObject.getJSONArray("tem");

                for(int i = 0; i<array.length(); i++) {
                    JSONObject o = array.getJSONObject(i);
                    ListItem item = new ListItem(
                            o.getString("name"),
                            o.getString("scientificname"),
                            o.getString("image_url")
                    );
                    listItems.add(item);
                }

                adapter = new MyAdapter(listItems, getActivity().getApplicationContext());
                recyclerView.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    Toast.makeText(getActivity().getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    requestQueue.add(stringRequest);
}
public ByobovateFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    recyclerView = (RecyclerView)getActivity().findViewById(R.id.recyclerView_obovate);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    listItems = new ArrayList<>();

    loadRecycleViewData();
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_byobovate, container, false);
}

0 个答案:

没有答案