我想使用数组适配器在ListView
中显示数据。当我运行程序时,我得到了NullPointerException
。错误发生在myarray.add(contactid +" - ");
完整错误是:
java.lang.NullPointerException:尝试调用虚方法' boolean java.util.ArrayList.add(java.lang.Object)'在com.android.jsonparsingtest2.MainActivity $ JSONTask.doInBackground(MainActivity.java:82)上的空对象引用 在com.android.jsonparsingtest2.MainActivity $ JSONTask.doInBackground(MainActivity.java:44)
MainActivity.java
public class MainActivity extends ActionBarActivity {
private ListView tvData;
private ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHit = (Button) findViewById(R.id.btnHit);
btnHit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new JSONTask().execute("http://a.nextput.com/offers/19/o/a0f252064bd27339607c7166d270d82a/f?aff_id=56556_18&im=25");
}
});
}
public class JSONTask extends AsyncTask<String, String, ArrayList<String>> {
ArrayList<String> myarray;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected ArrayList<String> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("offers");
//ArrayList<String> data = new ArrayList<>();
StringBuffer finalBufferData = new StringBuffer();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
String contactid = finalObject.getString("app_name");
//String custname = finalObject.getString("name");
//String custemail = finalObject.getString("email");
//String custgender = finalObject.getString("gender");
//String mobileno = finalObject.getString("mobile");
myarray.add(contactid+ " - ");
}
return myarray;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
// tvData.setText(result);
arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, result);
tvData = (ListView) findViewById(R.id.tvJsonItem);
tvData.setAdapter(arrayAdapter);
}
}
}
答案 0 :(得分:1)
ArrayList<String> myarray;
是不够的(仍为空)。
ArrayList<String> myarray = new ArrayList<>();
应该有用。
答案 1 :(得分:0)
您尚未初始化myarray
。这样的事情会起作用:
ArrayList<String> myarray;
@Override
protected void onPreExecute() {
super.onPreExecute();
myarray = new ArrayList<>();
}