我正在尝试解析 JSON 提要并将其显示到自定义列表视图中,但我收到 NullPointerException
I am trying to parse JSON feed and display it into a Custom Listview but I am getting NullPointerException
我正在尝试解析 JSON 提要并将其显示到自定义列表视图中,但出现 NullPointerException。这是我正在使用的代码,我使用了 volley 并解析了 response ,它在一个简单的 listview 上工作正常,但在自定义 listview 上有问题。请任何人解决这个问题,将不胜感激。
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;
}}
假设其他一切正常,您可以更正 NullPointerException(和 IndexOutOfBoundsException),方法是将 String[]
切换为 ArrayList<String>
。
问题是这两行没有初始化 String[]
,所以它们将为空。
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;
}}
我正在尝试解析 JSON 提要并将其显示到自定义列表视图中,但出现 NullPointerException。这是我正在使用的代码,我使用了 volley 并解析了 response ,它在一个简单的 listview 上工作正常,但在自定义 listview 上有问题。请任何人解决这个问题,将不胜感激。
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;
}}
假设其他一切正常,您可以更正 NullPointerException(和 IndexOutOfBoundsException),方法是将 String[]
切换为 ArrayList<String>
。
问题是这两行没有初始化 String[]
,所以它们将为空。
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;
}}