-1
我正在製作一個在數據庫中加載名稱的列表視圖。 CustomerID用於訪問名稱的詳細視圖。 我在Android Studio VDM中獲得輸出沒有任何問題。但是當我在手機上運行它時(Android 4.3),會出現以下錯誤。數據庫連接在Android Studio VDM中運行良好,但不在電話中
顯示java.lang.NullPointerException:嘗試在上com.tut.app.GetAllCustomerListViewAdapter.getCount(GetAllCustomerListViewAdapter.java:38)空對象引用調用虛擬方法 '詮釋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);
}
}
}
請幫助! :(