我正在尝试解析JSON提要并将其显示到自定义列表视图中,但我收到了NullPointerException。这里是我正在使用的代码,我使用了volley并解析了响应,它在一个简单的列表视图上正常工作,但是自定义列表视图存在问题。请任何人解决这个问题,将不胜感激。
public class News extends Activity {
ListView lv;
Context context;
String uri = "http://lodhisamaj.16mb.com/jsongen.php";
String[] newstitle;
String[] newsdesc;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.news);
lv = (ListView) findViewById(R.id.newslist);
Myadapter adapter = new Myadapter(this, newstitle, newsdesc);
lv.setAdapter(adapter);
RequestQueue rq = Volley.newRequestQueue(this);
JsonObjectRequest jreq = new JsonObjectRequest(
com.android.volley.Request.Method.GET, uri, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray title = response.getJSONArray("data");
for (int i = 0; i < title.length(); i++) {
String xy = title.getJSONObject(i).getString(
"Title");
String xy2 = title.getJSONObject(i).getString(
"Meassage");
xy = newstitle[i];
xy2 = newsdesc[i];
}
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(),
Toast.LENGTH_LONG).show();
}
});
rq.add(jreq);
}}
class Myadapter extends ArrayAdapter<String> {
Context context;
String[] titleArray;
String[] descArray;
public Myadapter(Context c, String[] newstitle, String[] newsdesc) {
super(c, R.layout.news_row, newstitle);
this.context = c;
this.titleArray = newstitle;
this.descArray = newsdesc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflator.inflate(R.layout.news_row, parent, false);
TextView myTitle = (TextView) row.findViewById(R.id.newstitle);
TextView mydec = (TextView) row.findViewById(R.id.newsdec);
myTitle.setText(titleArray[position]);
mydec.setText(descArray[position]);
return row;
}}
答案 0 :(得分:0)
假设其他一切正常,您可以通过将String[]
切换为ArrayList<String>
来纠正NullPointerException(和IndexOutOfBoundsException)。
问题是这两行没有初始化String[]
,所以它们将为null。
String[] newstitle;
String[] newsdesc;
此外,不确定这两行有意做什么......
xy = newstitle[i];
xy2 = newsdesc[i];
话虽如此,希望这对你有用。 (未测试)
public class News extends Activity {
ListView lv;
Context context;
String uri = "http://lodhisamaj.16mb.com/jsongen.php";
ArrayList<String> newstitle;
ArrayList<String> newsdesc;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.news);
// Initialize your lists so they are not null
newstitle = new ArrayList<String>();
newsdesc = new ArrayList<String>();
lv = (ListView) findViewById(R.id.newslist);
Myadapter adapter = new Myadapter(this, newstitle, newsdesc);
lv.setAdapter(adapter);
RequestQueue rq = Volley.newRequestQueue(this);
JsonObjectRequest jreq = new JsonObjectRequest(
com.android.volley.Request.Method.GET, uri, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray title = response.getJSONArray("data");
for (int i = 0; i < title.length(); i++) {
String xy = title.getJSONObject(i).getString(
"Title");
String xy2 = title.getJSONObject(i).getString(
"Meassage");
// Add your strings to your lists
newstitle.add(xy);
newsdesc.add(xy2);
}
} catch (JSONException e) {
e.printStackTrace(); // never ignore your errors
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(),
Toast.LENGTH_LONG).show();
}
});
rq.add(jreq);
}}
class Myadapter extends ArrayAdapter<String> {
Context context;
ArrayList<String> titleArray;
ArrayList<String> descArray;
public Myadapter(Context c, ArrayList<String> newstitle, ArrayList<String> newsdesc) {
super(c, R.layout.news_row, newstitle);
this.context = c;
this.titleArray = newstitle;
this.descArray = newsdesc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflator.inflate(R.layout.news_row, parent, false);
TextView myTitle = (TextView) row.findViewById(R.id.newstitle);
TextView mydec = (TextView) row.findViewById(R.id.newsdec);
myTitle.setText(titleArray.get(position));
mydec.setText(descArray.get(position));
return row;
}}