*错误* RecycleView到另一个片段

时间:2017-07-05 03:02:56

标签: android-fragments

嗨,大家好,谢谢你们 最初假设工作就像当我点击recyclerView中的任何项目时会将我带到另一个显示详细信息的片段。

每当我点击recyclerView中的项目时,都会立即崩溃。所以我想是我的onClick方法设置错误或其他什么。

/ **  * pending_Fragment  * /

public class pending_fragment扩展了Fragment工具    RecyclerViewAdapter.ItemClickCallBack,View.OnClickListener {

private static final String BUNDLE_EXTRAS = "BUNDLE_EXTRAS";
private static final String EXTRA_SUBMITDATE = "EXTRA_SUBMITDATE";
private static final String EXTRA_DERIVE = "EXTRA_DERIVE";
private static final String EXTRA_STARTEND = "EXTRA_STARTEND";
private static final String EXTRA_STATUS = "EXTRA_STATUS";

private RecyclerView recView;
private RecyclerViewAdapter adapter;
private List<ListItem> listData;
private TextView textViewView;

private String jsonString = "";

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    final String url = "http://52.187.24.214/medical_certificates/";
    new AsyncHTTPTask().execute(url);

    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_pending_fragment, container, false);

    recView = (RecyclerView) v.findViewById(R.id.rec_list);
    adapter = new RecyclerViewAdapter(listData, getActivity());

    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recView.setLayoutManager(layoutManager);
    adapter.setItemClickCallBack(this);

    return v;
}

@Override
public void onClick(View v) {
}

@Override
public void onItemClick(int p) {
    Intent i = new Intent(getActivity(), detailActivity.class);
    ListItem item = (ListItem) listData.get(p);
    Bundle extras = new Bundle();
    extras.putString(EXTRA_SUBMITDATE, "asd");
    extras.putString(EXTRA_DERIVE, "asdsad");
    extras.putString(EXTRA_STARTEND, "asdsad");
    extras.putString(EXTRA_STATUS, "asdsad");
    i.putExtra(BUNDLE_EXTRAS, extras);

    startActivity(i);
}

public class AsyncHTTPTask extends AsyncTask<String, Void, Integer>{

    @Override
    protected Integer doInBackground(String... params){
        Integer result = 0;
        HttpURLConnection urlConnection;
        try{
            URL url = new URL(params[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            int statusCode = urlConnection.getResponseCode();

            // 200 represents HTTP OK
            if (statusCode == 200){
                BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = r.readLine()) != null) {
                    response.append(line);
                }
                jsonString = response.toString();
                parseResult();
                result = 1; // Successful
            } else {
                result = 0; //"Failed to fetch data!";
            }
        } catch (Exception e) {
            Log.d("Exception Caught", e.getLocalizedMessage());
        }
        return result; //"Failed to fetch data!";
        }
    @Override
    protected void onPostExecute(Integer result) {

        super.onPostExecute(result);

        if (result == 1) {
            adapter = new RecyclerViewAdapter(listData, getActivity());
            //Setting the adapter
            recView.setAdapter(adapter);
        } else {
            Toast.makeText(getActivity(), "Failed to fetch data!", Toast.LENGTH_SHORT).show();
        }
    }

    //This method will parse the RAW data downloaded from the server
    private void parseResult() {

        try {

            JSONObject jsonObj = new JSONObject(jsonString);
            JSONArray wholeData = jsonObj.getJSONArray("data");
            listData = new ArrayList<>();

            for (int i = 0; i < wholeData.length(); i++) {
                JSONObject parentData = wholeData.getJSONObject(i);
                JSONObject childData = parentData.getJSONObject("attributes");
                ListItem item = new ListItem();
                item.setSubmissionDate(childData.getString("created-at"));
                item.setStartDate(childData.getString("start-date"));
                item.setStatus(childData.getString("status"));
                listData.add(item);

                //Log.e("Event Name", c.getString("eventName"));
            }

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

}

/ **  * RecylerViewAdapter  * /

public class RecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private List<ListItem> listData;
private LayoutInflater inflater;

//Interface, a communication alternative that doesn't hold memory
private ItemClickCallBack itemClickCallBack;

//Creates a layout inflater from the context
public RecyclerViewAdapter(List<ListItem> listData, Context c){
    this.inflater = LayoutInflater.from(c);
    this.listData = listData;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.list_item, parent, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ListItem item = listData.get(position);
    holder.submissionDate.setText(item.getSubmissionDate());
    holder.startDate.setText(item.getStartDate());
    holder.status.setText(item.getStatus());
}

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

public void setItemClickCallBack(final ItemClickCallBack itemClickCallBack){
    this.itemClickCallBack = itemClickCallBack;
}

public interface ItemClickCallBack {
    //if user clicks container then react accordingly
    void onItemClick(int p);
}

//updater
public void setListData(ArrayList<ListItem> exerciseList) {
    this.listData.clear();
    this.listData.addAll(exerciseList);
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    private TextView submissionDate;
    private TextView startDate;
    private TextView endDate;
    private TextView status;
    private TextView reject_reason;
    private View container;


    public ViewHolder(View itemView) {
        super(itemView);

        submissionDate = (TextView)itemView.findViewById(R.id.lbl_item_text);
        startDate = (TextView)itemView.findViewById(R.id.lbl_from_date_to_date);
        status = (TextView)itemView.findViewById(R.id.lbl_status);
        container = itemView.findViewById(R.id.cont_item_root);
        container.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        //Checks ID
        if (v.getId()== R.id.cont_item_root) {
            //Gets the position of item that was clicked
            itemClickCallBack.onItemClick((getAdapterPosition()));
        }
    }
}

}

致命的例外:主要                                                                                    处理:com.example.kaiying.mc_submission,PID:22831                                                                                    java.lang.NullPointerException:尝试调用接口方法&#39; void com.example.kaiying.mc_submission.student.adapter.RecyclerViewAdapter $ ItemClickCallBack.onItemClick(java.lang.String)&#39;在空对象引用上

enter image description here

1 个答案:

答案 0 :(得分:1)

更新onPostExecute(),如下所示

@Override
protected void onPostExecute(Integer result) {

    super.onPostExecute(result);

    if (result == 1) {
        adapter = new RecyclerViewAdapter(listData, getActivity());
        //Setting the adapter
        adapter.setItemClickCallBack(pending_fragment.this);
        recView.setAdapter(adapter);
    } else {
        Toast.makeText(getActivity(), "Failed to fetch data!", Toast.LENGTH_SHORT).show();
    }
}