我在android中有一些关于日历的问题。我想添加数据库中的事件。我基本上想要的是当我打开我的日历片段时,它将调用Web服务并从服务器获取数据,其中包括日期及其各自的事件,当我在日历中单击该日期时它向我显示指定日期的事件。我遇到的问题是:
它在此行date = sdf.parse(dates);
上显示一些错误,并显示
java.lang.NullPointerException
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1009)
at java.text.DateFormat.parse(DateFormat.java:553)
at com.example.aksystems.practiceportal.Calendar.showJSON(Calendar.java:118)
成功运行前两天,问题是它会在日历日生成两次点。示例:我的服务器响应是
[{"d":"a","events":"2017\/04\/13 18:20:43"},{"d":"a","events":"2017\/04\/10 18:20:40"}]`
所以它会将日期13指向一次,将日期指定为10次以后为什么?
Calendar.java
public class Calendar extends Fragment {
CalendarView calendarView;
private EditText editTextId;
private Button buttonGet;
private TextView textViewResult;
String dates,events;
private ProgressDialog loading;
CompactCalendarView compactCalendarView;
String id = "a";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = null;
public Calendar() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_calendar, container, false);
compactCalendarView = (CompactCalendarView) view.findViewById(R.id.compactcalendar_view);
editTextId = (EditText) view.findViewById(R.id.editTextId);
buttonGet = (Button)view. findViewById(R.id.buttonGet);
textViewResult = (TextView) view.findViewById(R.id.textViewResult);
getData();
return view;
}
private void getData() {
loading = ProgressDialog.show(getActivity(),"Please wait...","Fetching...",false,false);
String url = Config.DATA_URL+"?e="+id;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
private void showJSON(String response)
{
for (int i=0;i < response.length();i++)
{
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(i);
dates = collegeData.getString(Config.KEY_DATES);
events = collegeData.getString(Config.KEY_EVENTS);
//vc = collegeData.getString(Config.KEY_VC);*/
} catch (JSONException e) {
e.printStackTrace();
}
// textViewResult.setText(""+dates);
// String myDate = "2017/03/30 18:10:45";
try
{
date = sdf.parse(dates);
} catch (ParseException e) {
e.printStackTrace();
}
// Convert Date into milliseconds
assert date != null;
long millis = date.getTime();
Event ev1 = new Event(Color.RED, millis, events);
compactCalendarView.addEvent(ev1);
/* List<Event> events = compactCalendarView.getEvents(millis);
Toast.makeText(getActivity(), "" + events, Toast.LENGTH_SHORT).show();*/
compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener()
{
@Override
public void onDayClick(Date dateClicked) {
List<Event> events = compactCalendarView.getEvents(dateClicked);
// Log.d(TAG, "Day was clicked: " + dateClicked + " with events " + events);
Toast.makeText(getActivity(), "" + events, Toast.LENGTH_SHORT).show();
}
@Override
public void onMonthScroll(Date firstDayOfNewMonth)
{
// Log.d(TAG, "Month was scrolled to: " + firstDayOfNewMonth);
// Toast.makeText(getActivity(), "Month was scrolled to: " + firstDayOfNewMonth, Toast.LENGTH_SHORT).show();
}
});
}
}
}
答案 0 :(得分:1)
response.length()
返回的值超过两个,因此它将指向上一个日期中的多个事件。尝试使用JSONArray.size()
查找响应的长度。