我创建了一个应用程序,在其中我从Json Array获取信息。我解析并存储它。 现在我想在ListView中显示此信息,但我得到空指针异常。
SearchValue(存储数组信息的类)
public class SearchValues {
public String address,applications,counts,contactInfo,email,expSummary,gender,IjobReqId,area,city,code,country,description,expDate,hours,state,status,title,type,maxExp,minExp,noOfPos,postedon,religion,requestorName,category;
}
SearchJobsCustomList(自定义列表视图)
public class SearchJobsCustomList extends BaseAdapter {
Context c;
List<SearchValues> valueSearch;
public SearchJobsCustomList(Context c, List<SearchValues> valueSearch) {
super ();
this.c = c;
this.valueSearch = valueSearch;
}
@Override
public int getCount() {
return valueSearch.size ();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
SearchValues values = valueSearch.get (i);
if (view == null) {
view = LayoutInflater.from (c).inflate (R.layout.custom_search_jobs_lists, viewGroup, false);
TextView JobCode = (TextView) view.findViewById (R.id.tv_job_code);
TextView Category = (TextView) view.findViewById (R.id.tv_category);
TextView ExpYrs = (TextView) view.findViewById (R.id.tv_exp_yrs);
TextView ExpMnths = (TextView) view.findViewById (R.id.tv_exp_mnths);
TextView Date = (TextView) view.findViewById (R.id.tv_date);
JobCode.setText (values.code);
Category.setText (values.category);
ExpYrs.setText (values.minExp);
ExpMnths.setText (values.maxExp);
Date.setText (values.postedon);
}
return view;
}
}
SearchJobLists
public class SearchJobsList extends Activity {
SearchJobsCustomList searchJobsCustomList;
private ListView lvresources;
private Context c = this;
List<SearchValues> valuesS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.search_job_lists);
initialize ();
}
private void initialize() {
lvresources = (ListView) findViewById (R.id.listView);
searchJobsCustomList = new SearchJobsCustomList (c, valuesS);
lvresources.setAdapter (searchJobsCustomList);
}
}
SearchJobs(从哪里获得Json)
public class SearchJobsAsync extends AsyncTask<String, String, String> {
String response;
@Override
protected String doInBackground(String... strings) {
response = HttpRequest.post ("https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/FindJobForVendor").send ("Vendor_IEntity_Code=" + "89F00539-25EA-E311-853B-000C29762494" + "&Job_Code=" + "" + "&Job_Category=" + strCategory + "&Exp_Years_From=" + strYrs + "&Exp_Months_From=" + strMnths + "&Exp_Years_To=" + strToYrs + "&Exp_Months_To=" + strToMnths).body ();
//Vendor_IEntity_Code=string&Job_Code=string&Job_Category=string&Exp_Years_From=string&Exp_Months_From=string&Exp_Years_To=string&Exp_Months_To=string
response = response.replaceAll ("<[^>]*>", "").replaceAll ("\n", "");
Log.e ("Response", "" + response);
return response;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute (s);
SearchValues values = new SearchValues ();
try {
jsonObject = new JSONObject (s);
NewDataSet = jsonObject.getJSONObject ("NewDataSet");
if (NewDataSet == null) {
Toast.makeText (SearchJobs.this, "error", Toast.LENGTH_SHORT).show ();
} else if (NewDataSet.get ("Table") instanceof JSONObject) {
JSONObject table = NewDataSet.getJSONObject ("Table");
values.address = table.getString ("Address");
values.applications = table.getString ("Applications");
values.counts = table.getString ("Candidate_Counts");
values.contactInfo = table.getString ("Contact_No");
values.email = table.getString ("Email");
values.expSummary = table.getString ("Exp_Summary");
values.gender = table.getString ("Gender_Name");
values.IjobReqId = table.getString ("IJob_Request_ID");
values.area = table.getString ("Job_Area");
values.category = table.getString ("Job_Category");
values.city = table.getString ("Job_City");
values.code = table.getString ("Job_Code");
values.country = table.getString ("Job_Country");
values.description = table.getString ("Job_Desc");
values.expDate = table.getString ("Job_Exp_Date");
values.hours = table.getString ("Job_Hours");
values.state = table.getString ("Job_State");
values.status = table.getString ("Job_Status");
values.title = table.getString ("Job_Title");
values.type = table.getString ("Job_Type");
values.maxExp = table.getString ("Max_Exp");
values.minExp = table.getString ("Min_Exp");
values.noOfPos = table.getString ("No_Of_Pos");
values.postedon = table.getString ("Posted_On");
values.religion = table.getString ("Religion_Name");
values.requestorName = table.getString ("Requestor_Name");
} else if (NewDataSet.get ("Table") instanceof JSONArray) {
JSONArray tablearray = NewDataSet.getJSONArray ("Table");
for (int i = 0; i< tablearray.length (); i++) {
JSONObject table = tablearray.getJSONObject (i);
values.address = table.getString ("Address");
values.applications = table.getString ("Applications");
values.counts = table.getString ("Candidate_Counts");
values.contactInfo = table.getString ("Contact_No");
values.email = table.getString ("Email");
values.expSummary = table.getString ("Exp_Summary");
values.gender = table.getString ("Gender_Name");
values.IjobReqId = table.getString ("IJob_Request_ID");
values.area = table.getString ("Job_Area");
values.category = table.getString ("Job_Category");
values.city = table.getString ("Job_City");
values.code = table.getString ("Job_Code");
values.country = table.getString ("Job_Country");
values.description = table.getString ("Job_Desc");
values.expDate = table.getString ("Job_Exp_Date");
values.hours = table.getString ("Job_Hours");
values.state = table.getString ("Job_State");
values.status = table.getString ("Job_Status");
values.title = table.getString ("Job_Title");
values.type = table.getString ("Job_Type");
values.maxExp = table.getString ("Max_Exp");
values.minExp = table.getString ("Min_Exp");
values.noOfPos = table.getString ("No_Of_Pos");
values.postedon = table.getString ("Posted_On");
values.religion = table.getString ("Religion_Name");
values.requestorName = table.getString ("Requestor_Name");
}
}
} catch (JSONException e) {
e.printStackTrace ();
}
// Intent i = new Intent (SearchJobs.this, SearchJobsList.class);
// startActivity (i);
}
}
}
logcat的
Caused by: java.lang.NullPointerException
at com.example.jobs_on_call_adapter.SearchJobsCustomList.getCount(SearchJobsCustomList.java:31)
at android.widget.ListView.setAdapter(ListView.java:465)
at com.example.jobs_on_call_search_jobs.SearchJobsList.initialize(SearchJobsList.java:38)
at com.example.jobs_on_call_search_jobs.SearchJobsList.onCreate(SearchJobsList.java:29)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
at android.app.ActivityThread.access$600(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4787)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
现在我出错了,请纠正我。如果你能给我任何关于如何从JSON(数组或对象)存储信息的建议,并且在其他地方使用它。
答案 0 :(得分:0)
您应该在valuesS = new List<SearchValues>();
OnCreate
之前编写initialize ();
(不再有NullPointerException),并在解析JSON时在此List中添加一些数据。
答案 1 :(得分:0)
是的,当您创建SearchJobsCustomList的对象时,您还没有定义valuesS值,您刚刚定义了该类型的变量。
searchJobsCustomList = new SearchJobsCustomList(c,valuesS);
似乎你想把你的json数据显示为valuesS,为了命令它创建一个列表对象并从数据库或从你获得的json数据中推送你的所有值。
休息可以在列表视图中填充项目。
答案 2 :(得分:0)
你能不能在你的initialize()方法中改变它,因为你传递的是Searchvalues的null对象。
lvresources = (ListView) findViewById (R.id.listView);
SearchValues valuesS = new SearchValues();
searchJobsCustomList = new SearchJobsCustomList (c, valuesS);
lvresources.setAdapter (searchJobsCustomList); }