我正在创建一个listview,用于加载数据库中的名称。 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);
}
}
}
请帮忙! :(
答案 0 :(得分:0)
我发现什么是错的! :d 我的笔记本电脑已连接到Localhost。因此,它在运行应用程序时没有任何问题。但只有当我在服务器中托管php文件和数据库时,该应用程序在手机中完美无缺!希望这有助于某人!