【发布时间】:2015-04-12 15:50:28
【问题描述】:
我正在制作一个列表视图,用于加载数据库中的名称。 CustomerID 用于访问名称的详细视图。 我在 Android Studio VDM 中得到输出没有任何问题。但是当我在手机(Android 4.3)上运行它时,出现以下错误。
java.lang.NullPointerException:尝试在 com.tut.app.GetAllCustomerListViewAdapter.getCount(GetAllCustomerListViewAdapter.java:38) 的空对象引用上调用虚拟方法“int org.json.JSONArray.length()”
我的主要活动:
public class MainActivity extends ActionBarActivity {
private ListView GetAllCustomerListView;
private JSONArray jsonArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.GetAllCustomerListView = (ListView) this.findViewById(R.id.GetAllCustomerListView);
new GetAllCustomerTask().execute(new ApiConnector());
this.GetAllCustomerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try
{
// GEt the customer which was clicked
JSONObject customerClicked = jsonArray.getJSONObject(position);
// Send Customer ID
Intent showDetails = new Intent(getApplicationContext(),CustomerDetailsActivity.class);
showDetails.putExtra("CustomerID", customerClicked.getInt("id"));
startActivity(showDetails);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
});
}
public void setListAdapter(JSONArray jsonArray)
{
this.jsonArray = jsonArray;
this.GetAllCustomerListView.setAdapter(new GetAllCustomerListViewAdapter(jsonArray,this));
}
private class GetAllCustomerTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
@Override
protected JSONArray doInBackground(ApiConnector... params) {
// it is executed on Background thread
return params[0].GetAllCustomers();
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
setListAdapter(jsonArray);
}
}
}
请帮忙! :(
【问题讨论】:
标签: android json android-studio