我有以下问题。 一个日期选择器,我选择一个日期,这将被放入一个带有用户名的数组。用于通过在Parser中使用GetMemberPatrolStartTimes来获取用户开始时间的数组。这将返回JSON对象,然后将其发送到填充的巡逻历史记录中一个微调器。
我可以看到微调器被填充但是只要我点击向下拖动微调器我就会得到文件中附加的错误。
现在我可以读取错误,但用它来解决问题我无法做对。
感谢所有帮助,指导和任何建设性的批评
错误:java.lang.NullPointerException:尝试调用虚方法' java.lang.String java.lang.Object.toString()'在空对象引用上
这是在UncaughtException方法中捕获的 的 eyeWatchApi.java
public JSONObject GetMemberPatrolStartTimes(ArrayList<String> details)throws Exception {
JSONObject result = null;
JSONObject o = new JSONObject();
JSONObject p = new JSONObject();
o.put("interface","eyeWatchApi");
o.put("method", "GetMemberPatrolStartTimes");
p.put("details",mapObject(details));
o.put("parameters", p);
String s = o.toString();
String r = load(s);
result = new JSONObject(r);
return result;
}
Parser.java
public Object GetPatrolHistoryStartTimes(JSONObject object){
String[] id_Array;
String[] time_Array;
try{
JSONArray a = object.getJSONArray("Value");
id_Array = new String[a.length()];
time_Array = new String[a.length()];
for (int i = 0; i < a.length(); i++) {
JSONObject b = a.getJSONObject(i);
id_Array[i]=b.getString("startID");
time_Array[i]=b.getString("startTime");
return new Object[]{id_Array,time_Array};
}
}catch (JSONException e){
return e;
}catch (Exception e){
return e;
}
return null;
}
Patrol_History.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.patrol_history,container,false);
timeSpinner = (Spinner) view.findViewById(R.id.TimeSpinner);
timeSpinner.setOnItemSelectedListener(this);
}
@Override
public void onClick(View view)
{
switch(view.getId())
{
case R.id.load_polly_btn :
{
drawpoliline();
break;
}
case R.id.select_date_txt :
{
Date_Pick_Dialog picker = new Date_Pick_Dialog();
picker.setDatePickerDialogFragmentEvents(this);
picker.show(getFragmentManager(), "datePicker");
break;
}
default:
{
break;
}
}
}
@Override
public void onDateSelected(String _date) {
patrolHistoryManager = new PatrolHistoryManager(getActivity());
patrolHistoryManager.setDate(_date);
selectDate_tv.setText(_date);
date = _date;
new AsyncSendDetails().execute(date);
//new AsyncGetAvailibleSlots().execute(_date);
}
public class AsyncSendDetails extends AsyncTask<String, Void, Object[]> {
@Override
protected Object[] doInBackground(String... params) {
eyeWatchApi api = new eyeWatchApi();
Parser parser = new Parser();
detailsList = new ArrayList<String>();
detailsList.add(userName);
detailsList.add(params[0]);
Object[] ob = new Object[0];
try {
ob = (Object[]) parser.GetPatrolHistoryStartTimes(api.GetMemberPatrolStartTimes(detailsList));
} catch (Exception e) {
e.printStackTrace();
}
return ob;
}
@Override
protected void onPostExecute(Object[] ob) {
if(ob != null)
{
timeID = (String[]) ob[0];
times = (String[]) ob[1];
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), R.layout.countries_spinner_item, times);
dataAdapter.setDropDownViewResource(R.layout.countries_spinner_item_droplist);
timeSpinner.setAdapter(dataAdapter);
}
else
{
Toast.makeText(getActivity(),"No Slots Available",Toast.LENGTH_LONG).show();
}
}
}
布局文件
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Select Date"
android:textAlignment="center"
android:textColor="#d32f2f"
android:cursorVisible="false"
android:shadowColor="@color/black"
android:id="@+id/select_date_txt"
android:layout_gravity="top|right"
android:text="02/05/2016"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/white"
android:layout_below="@+id/select_date_txt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/relativeLayout2" />
<Spinner
android:id="@+id/TimeSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#d32f2f"
android:layout_below="@+id/select_date_txt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</Spinner>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:paddingBottom="2dp"
android:background="@color/primary"
android:id="@+id/relativeLayout"
android:layout_below="@+id/TimeSpinner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/relativeLayout">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_weight="0.73"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</fragment>
<android.support.v7.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load Lines"
android:id="@+id/load_polly_btn"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:0)
After almost a week of struggling i have just found the answer so i will be keeping it on here for all to see if there is a need for it
**Parser.java**
public Object GetPatrolHistoryStartTimes(JSONObject object){
String[] id_Array;
String[] time_Array;
try{
JSONArray a = object.getJSONArray("Value");
id_Array = new String[a.length()];
time_Array = new String[a.length()];
for (int i = 0; i < a.length(); i++) {
JSONObject b = a.getJSONObject(i);
id_Array[i]=b.getString("startID");
time_Array[i]=b.getString("startTime");
}
return new Object[]{id_Array,time_Array};
}catch (JSONException e){
return e;
}catch (Exception e){
return e;
}
return null;
}
The problem was that my return of the object should not have been inside the for statement but rather after it, after the loop has been completed