我需要在项目点击ListView中调用一个方法。该应用程序在运行时停止响应。我试图在类GetSubVitals(另一个类)中运行一个名为getSubVitals的方法。我该怎么称呼这种方法?任何帮助将不胜感激。
public class VitalListActivity extends ActionBarActivity {
GetSubvitals getSub;
FragmentManager fm = getSupportFragmentManager();
public static List<String> vitalList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vital_list);
Intent intent = getIntent();
String status = intent.getStringExtra(LoginActivity.EXTRA_MESSAGE);
if(status.equals("true")){
new GetParentVitalName().execute();
//Log.d("check list",vitalList.get(0));
Log.d("Tag", "Execute complete");
//populateListView();
Log.d("Tag", "Populate complete");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_vital_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void populateListView(List<String> vitalList) {
ArrayAdapter<String> adapter = new MyListAdapter(vitalList);
ListView list = (ListView) findViewById(R.id.vitalsListView);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<String>{
public MyListAdapter(List<String> vitalList){
super(VitalListActivity.this, R.layout.vital_list_layout, vitalList);
Log.d("Tag", "super okay");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
//Making sure we've a view to work with(may have been given null
Log.d("TAG","Inside get view");
View itemView = convertView;
if (itemView == null)
itemView = getLayoutInflater().inflate(R.layout.vital_list_layout, parent, false);
String currentVital = vitalList.get(position);
//Fill the view
TextView vitalText = (TextView) itemView.findViewById((R.id.vitalTextView));
vitalText.setText(currentVital);
return itemView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return vitalList.size();
}
}
private class GetParentVitalName extends AsyncTask<String, String, String> {
protected String doInBackground(String... params){
String responseText = null;
HttpClient httpClient = ServiceHelper.getHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://test.gogch.com/gch-restful/vitals/custome/added/parentvitals");
Log.d("KMN", "Sucess");
try {
HttpResponse response = httpClient.execute(httpGet);
Log.d("DFGH","Success again.");
int statusCode = response.getStatusLine().getStatusCode();
Log.d("Http Post Response V", Integer.toString(statusCode));
responseText = EntityUtils.toString(response.getEntity());
Log.d("Http Post Response V", responseText);
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
return responseText;
}
protected void onPostExecute(String responseText){
if (responseText != null){
try {
JSONArray vitalsArray = new JSONArray(responseText);
for(int i =0;i<vitalsArray.length();i++){
JSONObject vitalsObject = vitalsArray.getJSONObject(i);
String parentVitalName = vitalsObject.getString("parentVitalName");
vitalList.add(parentVitalName);
Log.d("YGVJ",vitalList.get(i));
}
populateListView(vitalList);
registerListClick();
}
catch (JSONException e){
e.printStackTrace();
}
}
}
}
private void registerListClick(){
ListView list = (ListView) findViewById(R.id.vitalsListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
String vitalName = vitalList.get(position);
JSONArray subVitalsArray = null;
Log.d("HGFD","sadsadsad");
String responseText = getSub.getSubVitals(vitalName);
Log.d("HGFD",responseText);
if (responseText != null) {
try {
JSONArray vitalsArray = new JSONArray(responseText);
for (int i = 0; i < vitalsArray.length(); i++) {
JSONObject vitalsObject = vitalsArray.getJSONObject(i);
String parentVitalName = vitalsObject.getString("parentVitalName");
if (parentVitalName.equals(vitalName)) {
subVitalsArray = vitalsObject.getJSONArray("subVitals");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
ArrayList<SubVitals> subVitalList = new ArrayList<SubVitals>();
for(int i =0; i<subVitalsArray.length();i++){
try {
//map.clear();
JSONObject obj = subVitalsArray.getJSONObject(i);
String unit = obj.getString("unit");
String subVitalId = obj.getString("subVitalId");
String subVitalName = obj.getString("subVitalName");
String unitId = obj.getString("unitId");
SubVitals subVitals = new SubVitals();
subVitals.setUnit(unit);
subVitals.setSubVitalId(subVitalId);
subVitals.setSubVitalName(subVitalName);
subVitals.setUnitId(unitId);
//map.put("unit",unit);
//map.put("subVitalId",subVitalId);
//map.put("subVitalName", subVitalName);
//map.put("unitId", unitId);
subVitalList.add(subVitals);
}
catch(JSONException e){
e.printStackTrace();
}
}
//DFragment dFragment = DFragment.newInstance(subVitalList);
// Show DialogFragment
//dFragment.show(fm, "dialog");
//String message = "You clicked" + vitalName;
//Toast.makeText(VitalListActivity.this,message,Toast.LENGTH_LONG).show();
}
});
}
}
这就是logcat所说的:
04-07 18:31:46.759 29734-29734/com.cinch.getvitalsapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.cinch.getvitalsapp, PID: 29734
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.cinch.getvitalsapp.tasks.GetSubvitals.getSubVitals(java.lang.String)' on a null object reference
at com.cinch.getvitalsapp.activities.VitalListActivity$1.onItemClick(VitalListActivity.java:186)
答案 0 :(得分:1)
根据错误输出,你的getSub变量为null ....
- &GT;要么将getSubVitals方法设为静态,要么初始化getSub