I'm pretty new to android dev and I need some help.
I'm building an agenda that loads the information from a JSON then a ListView is inflated with a custom adapter. I've done this and works just fine.
My problem is the following when I click a contact another Activity is loaded with more information about the user, using the same JSON. I debug it and it recieves the information like this:
Example Item: [{"id":1,"name":"Leanne Graham","hobby":"Play soccer","address":"Kulas Light, Gwenborough","phone":"1-770-736-8031 x56442"}]
Because I sent the information as a JSONObject I cast it to be a JSONArray, but when I pass that array to my requestComplete my app breaks.
The error is:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
/**Main activity onclick listener*/
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("POSITION: " + position);
JSONObject jsonObject = (JSONObject)JSONadapter.getItem(position);
Intent intent = new Intent(this, InfoActivity.class);
String pos_json = jsonObject.toString();
intent.putExtra("pos_json",pos_json);
startActivity(intent);
}
/**Info activity*/
public class InfoActivity extends AppCompatActivity implements JSONRequest.JSONCallback {
AdapterInfo JSONAdapter;
private ListView listInfo;
private JSONObject json_object;
private JSONArray arrayMain;
private ArrayList<String> jsonarray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
JSONArray array = new JSONArray();
try {
json_object = new JSONObject(getIntent().getStringExtra("pos_json"));
arrayMain = array.put(json_object);
System.out.println("Example Item: "+ arrayMain.toString());
System.out.println(arrayMain.getClass().getName());
} catch (JSONException e) {
e.printStackTrace();
}
requestComplete(arrayMain);
this.listInfo = (ListView) findViewById(R.id.listView2);
}
#Override
public void requestComplete(JSONArray array) {
JSONAdapter = new AdapterInfo(InfoActivity.this,array);
this.listInfo.setAdapter(JSONAdapter);
}
/**Adapter*/
public class AdapterInfo extends BaseAdapter{
private JSONArray array;
private Activity infoAct;
public AdapterInfo(Activity infoAct, JSONArray array){
this.array = array;
this.infoAct = infoAct;
}
#Override
public int getCount() {
if(array == null){
return 0;
}else{
return array.length();
}
}
#Override
public JSONObject getItem(int position) {
if(array == null){
return null;
}else{
return array.optJSONObject(position);
}
}
#Override
public long getItemId(int position) {
JSONObject object = getItem(position);
return object.optLong("id");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = infoAct.getLayoutInflater().inflate(R.layout.row,null);
}
TextView name = (TextView)convertView.findViewById(R.id.infoName);
TextView hobby = (TextView)convertView.findViewById(R.id.infoHobby);
TextView address = (TextView)convertView.findViewById(R.id.infoAddress);
TextView phone = (TextView)convertView.findViewById(R.id.infoPhone);
JSONObject json_data = getItem(position);
if(json_data != null){
try {
String nombre = json_data.getString("name");
String pasatiempo = json_data.getString("hobby");
String direccion = json_data.getString("address");
String telefono = json_data.getString("phone");
name.setText(nombre);
hobby.setText(pasatiempo);
address.setText(direccion);
phone.setText(telefono);
} catch (JSONException e) {
e.printStackTrace();
}
}
return convertView;
}}
/**JSONRequest*/
public class JSONRequest extends AsyncTask<String, Void, JSONArray> {
private JSONCallback callback;
public JSONRequest(JSONCallback callback){
this.callback = callback;
}
#Override
protected JSONArray doInBackground(String... params) {
URLConnection connection = null;
BufferedReader br = null;
JSONArray result = null;
try{
URL url = new URL(params[0]);
connection = (URLConnection) url.openConnection();
InputStream is = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
String line = "";
while((line = br.readLine()) != null){
builder.append(line);
}
result = new JSONArray(builder.toString());
}catch (Exception e) {
e.printStackTrace();
} finally {
try{
if(br != null) br.close();
}catch(Exception e) {
e.printStackTrace();
}
}
return result;
}
#Override
protected void onPostExecute(JSONArray jsonArray) {
super.onPostExecute(jsonArray);
callback.requestComplete(jsonArray);
}
public interface JSONCallback{
void requestComplete(JSONArray array);
}}
Your code:
requestComplete(arrayMain);
this.listInfo = (ListView) findViewById(R.id.listView2);
requestComplete() uses this.listInfo instance but this.listInfo is null because it is set after requestComplete(). So you need to switch their order.
this.listInfo = (ListView) findViewById(R.id.listView2);
requestComplete(arrayMain);
It is better if you just put it right after call to setContentView() just to make sure this.listInfo holds valid ListView instance.
Related
Sometimes it shows data, sometimes it shows nothing.
public class HomeFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.home_fragment, container, false);
arraylist1 = new ArrayList<>();
listOfNews = (ListView) view.findViewById(R.id.listView);
runOnUiThread(new Runnable() {
#Override
public void run() {
new ReadJSON().execute("url");
}
});
return view;
}
public class ReadJSON extends AsyncTask<String, Integer, String> {
#Override
protected String doInBackground(String... url) {
String st = readURL(url[0]);
Log.d("st", st);
return st;
}
#Override
protected void onPostExecute(String content) {
try {
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
Log.d(i+":jsonArray object", jsonArray.toString() + " size= " + jsonArray.length());
JSONObject productObject = jsonArray.getJSONObject(i);
JSONObject img = productObject.getJSONObject("image");
JSONObject da = img.getJSONObject("data");
arraylist1.add(new HotNews(
da.getString("filename"),
productObject.getString("title"),
productObject.getString("article_by"),
productObject.getString("date_publish"),
productObject.getString("short_description")
));
}
Log.d("jsonArray news list1", arraylist1.size() + "");
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new ListViewAdapter(view.getContext(), R.layout.list_view_adapter, arraylist1);
listOfNews.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
private static String readURL(String theUrl) {
StringBuilder content = new StringBuilder();
try {
//create url object
URL url = new URL(theUrl);
//create url connection
URLConnection urlConnection = url.openConnection();
//wrap the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
}
public class ListViewAdapter extends ArrayAdapter<HotNews> {
public ListViewAdapter(Context context, int resource, ArrayList<HotNews> arrayList) {
super(context, resource,arrayList);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.arrayList = arrayList;
this.context = context;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.list_view_adapter, parent, false);
}
image = (ImageView) v.findViewById(R.id.imagetitle);
title = (TextView) v.findViewById(R.id.txtTitle);
doer = (TextView) v.findViewById(R.id.doer);
date = (TextView) v.findViewById(R.id.txtdate);
text = (TextView) v.findViewById(R.id.text);
final HotNews news = getItem(position);
Picasso.with(getContext()).load("url" + news.getImage()).noFade().into(image);
title.setText(news.getTitle());
doer.setText(news.getDoer());
date.setText(news.getDate());
text.setText(news.getContent());
return v;
}
}
try this code :
public class HomeFragment extends Fragment {
private View view;
private ArrayList<HotNews> arraylist1;
private ListView listOfNews;
private int SUCCESS = 1;
private int FAILS = 0;
private int NO_DATA = 2;
private int ERROR = -1;
private ListViewAdapter adapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.home_fragment, container, false);
arraylist1 = new ArrayList<>();
listOfNews = (ListView) view.findViewById(R.id.listView);
adapter = new ListViewAdapter();
listOfNews.setAdapter(adapter);
new ReadJSON().execute("url");
return view;
}
public class ReadJSON extends AsyncTask<String, Integer, Integer> {
#Override
protected Integer doInBackground(String... url) {
String response = readURL(url[0]);
Log.d("response ", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("data");
arraylist1.clear();
if(jsonArray.length()>0) {
for (int i = 0; i < jsonArray.length(); i++) {
Log.d(i + ":jsonArray object", jsonArray.toString() + " size= " + jsonArray.length());
JSONObject productObject = jsonArray.getJSONObject(i);
JSONObject img = productObject.getJSONObject("image");
JSONObject da = img.getJSONObject("data");
arraylist1.add(new HotNews(
da.getString("filename"),
productObject.getString("title"),
productObject.getString("article_by"),
productObject.getString("date_publish"),
productObject.getString("short_description")
));
}
}else{
return NO_DATA;
}
Log.d("jsonArray news list1", arraylist1.size() + "");
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
return SUCCESS;
}
#Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
Log.d("parseResponse", integer + "");
if(integer==SUCCESS){
adapter.notifyDataSetChanged();
}else if(integer==ERROR){
Toast.makeText(getActivity(),"Server or Exception Error!", Toast.LENGTH_SHORT).show();
}else if(integer==NO_DATA){
Toast.makeText(getActivity(),"No Data!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(),"Some thing went wrong!", Toast.LENGTH_SHORT).show();
}
}
}
class ListViewAdapter extends BaseAdapter{
#Override
public int getCount() {
return arraylist1.size();
}
#Override
public Object getItem(int position) {
return arraylist1.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ListViewHolder holder;
if (v == null) {
v = LayoutInflater.from(getActivity()).inflate(R.layout.list_view_adapter, parent, false);
holder = new ListViewHolder(v);
v.setTag(holder);
}else{
holder = (ListViewHolder)v.getTag();
}
final HotNews news = arraylist1.get(position);
holder.title.setText(news.getTitle());
holder.doer.setText(news.getDoer());
holder.date.setText(news.getDate());
holder.text.setText(news.getContent());
Picasso.with(getContext()).load("url" + news.getImage()).noFade().into(holder.image);
return v;
}
class ListViewHolder {
private ImageView image;
private TextView title,doer,date,text;
public ListViewHolder(View v){
image = (ImageView) v.findViewById(R.id.imagetitle);
title = (TextView) v.findViewById(R.id.txtTitle);
doer = (TextView) v.findViewById(R.id.doer);
date = (TextView) v.findViewById(R.id.txtdate);
text = (TextView) v.findViewById(R.id.text);
}
}
}
private static String readURL(String theUrl) {
StringBuilder content = new StringBuilder();
try {
//create url object
URL url = new URL(theUrl);
//create url connection
URLConnection urlConnection = url.openConnection();
//wrap the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
}
catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
}
You are seeing this error, which is crashing the entire loop. You may be requesting 10 objects, but at the first object without that image attribute, it is exiting.
W/System.err: org.json.JSONException: No value for image
So, recommendation (other than using Retrofit to manage your JSON API's) would be to use the optType() methods rather than the getType() methods of the JSON objects.
For example,
JSONObject img = productObject.optJSONObject("image");
This will set img to null when the "image" attribute does not exist.
Just be aware of that later in case you have a NullPointerException when trying to use img on this line
JSONObject da = img.getJSONObject("data");
I've currently got an application that pulls data from a mysql database and displays it in raw JSON format. I'm currently working on pushing this data into a String variable and displaying it on a Listview on a specific activity.
Problem is, when trying to display this data, my Listview is not populating; I'm sure the variable is not empty as the if statement would have captured this.
Here is snippet of MainActivity code:
//Methods to grab information from abhandym_DB database
public void getJSON(View view){
new BackgroundTask().execute();
}
public void parseJSON(View view){
if(JSON_String==null){
Toast.makeText(getApplicationContext(), "First Get Json", Toast.LENGTH_LONG).show();
}else{
Intent intent = new Intent(this,Test.class);
intent.putExtra("JSON_Data",JSON_String);
startActivity(intent);
}
}
class BackgroundTask extends AsyncTask<Void,Void,String>{
String json_url;
#Override
protected void onPreExecute() {
json_url = "http://abhandyman.x10host.com/json_get_data.php";
}
#Override
protected String doInBackground(Void... params) {
try {
URL url = new URL(json_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream inputSteam = httpURLConnection.getInputStream();
BufferedReader buffereredReader = new BufferedReader(new InputStreamReader(inputSteam));
StringBuilder stringBuilder = new StringBuilder();
while((JSON_String = buffereredReader.readLine())!=null){
stringBuilder.append(JSON_String+"\n");
}
buffereredReader.close();
inputSteam.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(String result) {
TextView textView = (TextView)findViewById(R.id.fragment1_textview_JSONAPPEAR);
textView.setText(result);
JSON_String = result;
}
}
Here is the code for my Test.java
public class Test extends AppCompatActivity {
String JSON_String;
JSONObject jsonObject;
JSONArray jsonArray;
DataAdapter dataAdapter;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
listView = (ListView)findViewById(R.id.test_listView);
dataAdapter = new DataAdapter(this, R.layout.row_layout);
listView.setAdapter(dataAdapter);
JSON_String = getIntent().getExtras().getString("JSON_Data");
try {
jsonObject = new JSONObject(JSON_String);
jsonArray = jsonObject.getJSONArray("server_response");
int count = 0;
String jobid,problem,resolution;
while(count<jsonObject.length()){
JSONObject JO = jsonArray.getJSONObject(count);
jobid = JO.getString("jobid");
problem = JO.getString("problem");
resolution = JO.getString("resolution");
Data data = new Data(jobid,problem,resolution);
dataAdapter.add(data);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Here is the code for my DataAdapter:
public class DataAdapter extends ArrayAdapter{
List list = new ArrayList();
public DataAdapter(Context context, int resource) {
super(context, resource);
}
public void add(Data object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
DataHolder dataHolder;
if(row == null){
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
dataHolder = new DataHolder();
dataHolder.tx_jobid = (TextView) row.findViewById(R.id.tx_jobid);
dataHolder.tx_problem = (TextView) row.findViewById(R.id.tx_problem);
dataHolder.tx_resolution = (TextView) row.findViewById(R.id.tx_resolution);
row.setTag(dataHolder);
}else{
dataHolder = (DataHolder)row.getTag();
}
Data data = (Data)this.getItem(position);
dataHolder.tx_jobid.setText(data.getJobid());
dataHolder.tx_problem.setText(data.getProblem());
dataHolder.tx_resolution.setText(data.getResolution());
return row;
}
static class DataHolder{
TextView tx_jobid,tx_problem,tx_resolution;
}
}
and here is what it displays when clicking on "Parse JSON" button.
listView empty after population
Any help or advise on why its not displaying would be much appreciated!
Thanks in advance!
your problem seems to be here :
while(count<jsonObject.length()){
you're not looping using the number of array elements but using the number of mapped key:value object which is one (the "server_response") , you have to change this line to :
while(count<jsonArray.length()){
,
you have just the first element showing because jsonObject.length() will return 1 since it have just one element.
from the doc, JSONObject, length() method:
Returns the number of name/value mappings in this object.
and in your case you have just one name/value mapped ("server_response":[array items...])
Check in Test.java. I think You are setting the adapter to the listview before adding data to it
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
listView = (ListView)findViewById(R.id.test_listView);
dataAdapter = new DataAdapter(this, R.layout.row_layout);
JSON_String = getIntent().getExtras().getString("JSON_Data");
try {
jsonObject = new JSONObject(JSON_String);
jsonArray = jsonObject.getJSONArray("server_response");
int count = 0;
String jobid,problem,resolution;
while(count<jsonObject.length()){
JSONObject JO = jsonArray.getJSONObject(count);
jobid = JO.getString("jobid");
problem = JO.getString("problem");
resolution = JO.getString("resolution");
Data data = new Data(jobid,problem,resolution);
dataAdapter.add(data);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
listView.setAdapter(dataAdapter); //change effected
}
This app is supposed to parse some JSON data (hard coded for now) from the Google Books API, and pass an ArrayList of Books to the adapter that will display it on a ListView. The problem I have is that the JSON parse is returning null instead of the parsed data.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ProgressBar pBar;
List<MyTask> tasks;
ArrayList<Book> bookList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pBar = (ProgressBar) findViewById(R.id.progressBar);
pBar.setVisibility(View.INVISIBLE);
Button sButton = (Button) findViewById(R.id.s_button);
sButton.setOnClickListener(this);
tasks = new ArrayList<>();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.s_button: {
if (isOnline()) {
new MyTask().execute("https://www.googleapis.com/books/v1/volumes?q=millionare"); //https://www.googleapis.com/books/v1/volumes?q=soft+skills
} else {
Toast.makeText(this, "Connection failed", Toast.LENGTH_LONG).show();
}
break;
}
}
}
protected boolean isOnline() {
ConnectivityManager connectManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
private class MyTask extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return HttpManager.getData(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
#Override
protected void onPostExecute(String result) {
bookList = BookJSONParser.parseFeed(result);
updateDisplay();
}
}
protected void updateDisplay() {
BookAdapter adapter = new BookAdapter(this, bookList);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
}
public class BookJSONParser {
public static ArrayList<Book> parseFeed(String content) {
try {
JSONArray jsonArray = new JSONArray(content);
ArrayList<Book> bookList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String name = object.getString("title").toString();
Book book = new Book(name);
bookList.add(book);
}
return bookList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}
public class BookAdapter extends ArrayAdapter<Book> {
public BookAdapter(Context context, ArrayList<Book> bookList) {
super(context, 0, bookList);
}
#Override
public View getView(int position, View convertedView, ViewGroup parent) {
View listItemView = convertedView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Book currentBook = getItem(position);
TextView locationName = (TextView) listItemView.findViewById(R.id.book_title);
locationName.setText(currentBook.getTittle());
TextView locationAddress = (TextView) listItemView.findViewById(R.id.book_author);
locationAddress.setText(currentBook.getAuthor());
return listItemView;
}
}
public class HttpManager {
public static String getData(String myUrl) throws IOException {
// BufferedReader reader = null;
InputStream inputStream = null;
int len = 10000;
try {
URL url = new URL(myUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000 /* milliseconds */);
connection.setConnectTimeout(15000 /* milliseconds */);
connection.setRequestMethod("GET");
connection.setDoInput(true);
// Starts the query
connection.connect();
int response = connection.getResponseCode();
inputStream = connection.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(inputStream, len);
return contentAsString;
// Makes sure that the InputStream inputStream closed after the app inputStream
// finished using it.
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
// Reads an InputStream and converts it to a String.
public static String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
}
public class Book {
private String mTittle;
/**
* This is the constructor.
* #param title is the book title being passed in.
*/
public Book(String title) {
mTittle = title;
}
public String getTittle() {
return mTittle;
}
public void setTittle(String tittle) {
mTittle = tittle;
}
}
FATAL EXCEPTION: main
Process: com.narvin.android.booklisting, PID: 3278
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
at android.widget.ListView.setAdapter(ListView.java:502)
at com.narvin.android.booklisting.MainActivity.updateDisplay(MainActivity.java:113)
at com.narvin.android.booklisting.MainActivity$MyTask.onPostExecute(MainActivity.java:100)
at com.narvin.android.booklisting.MainActivity$MyTask.onPostExecute(MainActivity.java:79)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
The issue is that one of the arguments in BookAdapter adapter = new BookAdapter(this, bookList); is null for some reason. Try passing bookList as an argument to updateDisplay and checking whether it's not null.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ProgressBar pBar;
List<MyTask> tasks;
ArrayList<Book> bookList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pBar = (ProgressBar) findViewById(R.id.progressBar);
pBar.setVisibility(View.INVISIBLE);
Button sButton = (Button) findViewById(R.id.s_button);
sButton.setOnClickListener(this);
tasks = new ArrayList<>();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.s_button: {
if (isOnline()) {
new MyTask().execute("https://www.googleapis.com/books/v1/volumes?q=millionare"); //https://www.googleapis.com/books/v1/volumes?q=soft+skills
} else {
Toast.makeText(this, "Connection failed", Toast.LENGTH_LONG).show();
}
break;
}
}
}
protected boolean isOnline() {
ConnectivityManager connectManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
private class MyTask extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return HttpManager.getData(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
#Override
protected void onPostExecute(String result) {
ArrayList<Book> bookList = BookJSONParser.parseFeed(result);
updateDisplay(bookList);
}
}
protected void updateDisplay(ArrayList<Book> bookList) {
if (bookList != null){
BookAdapter adapter = new BookAdapter(this, bookList);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
}
}
It would appear you are getting a JSONParseException... therefore causing a NullPointerExpcetion for the List into the Adapter
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
That is your error, here is how you get it
public static ArrayList<Book> parseFeed(String content) {
try {
JSONArray jsonArray = new JSONArray(content); // <-- Throws an error
ArrayList<Book> bookList = new ArrayList<>();
// Stuff...
return bookList;
} catch (JSONException e) {
e.printStackTrace();
return null; // <----- Null is returned
}
And you use that null value here
#Override
protected void onPostExecute(String result) {
bookList = BookJSONParser.parseFeed(result);
updateDisplay();
}
Followed by
protected void updateDisplay() {
BookAdapter adapter = new BookAdapter(this, bookList); // <-- Null here
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
So, the way to fix that NullPointerExpception is to always return an ArrayList
ArrayList<Book> bookList = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(content);
// Stuff...
} catch (JSONException e) {
e.printStackTrace();
}
return bookList;
to get json string from url you should do it like that
String content = new MyTask()
.execute("https://www.googleapis.com/books/v1/volumes?q=millionare")
.get();
//pass the content to BookJSONParser class
booklist = new BookJSONParser().parseFeed(content);
updateDisplay();
what you get from the url you provided is NOT jsonArray it's a jsonobject
so I think this code will work "assuming that you did everything else correctly"
JSONObject o = new JSONObject(content);
JSONArray jsonArray = o.getJSONArray("items");
the you can do the for loop
I am developing app just like YOUTUBE. I am getting data from the server and showing it in listview. Because data is too big so i want to restrict list view to 5 items and then when I scroll down to the bottom of listview it should add 5 more item.
I am calling ASyncTask from fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(R.layout.tab_fragment_2, container, false);
listView = (ListView) view.findViewById(R.id.myList);
New.jsonAsyncTask.execute("my url select_video.php?");
}
Now I have separate JsonAsyncTask Class because I am calling it from 3 fragments just like above mentioned way..
public class JSONAsyncTask extends AsyncTask<String, String, List<SetDatails>> {
ListView listView1;
Context context1;
List<SetDatails> videoLiast1;
private static int tab_id;
public JSONAsyncTask(ListView listView, List<SetDatails> videoLiast,Context context,int id) {
this.listView1 = listView;
this.context1 = context;
this.videoLiast1 = videoLiast;
tab_id=id;
}
#Override
protected List<SetDatails> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader myReader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream myInputStream = connection.getInputStream();
myReader = new BufferedReader(new InputStreamReader(myInputStream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = myReader.readLine()) != null) {
buffer.append(line);
}
String Json_data = buffer.toString();
videoLiast1 = new ArrayList<>();
JSONObject parentObj = new JSONObject(Json_data);
JSONArray dataArray = parentObj.getJSONArray("data");
JSONObject tabObject = dataArray.getJSONObject(tab_id);
JSONArray videoArray = tabObject.getJSONArray("videos");
for (int i = 0; i < videoArray.length(); i++) {
int len=dataArray.length();
SetDatails setDatails = new SetDatails();
JSONObject arrayChild = videoArray.getJSONObject(i);
String vid_ID = arrayChild.getString("id");
setDatails.setVidID(vid_ID);
String movie_title = arrayChild.getString("name");
setDatails.setTitle(movie_title);
String movie_catagory = arrayChild.getString("category_name");
setDatails.setGenre(movie_catagory);
videoLiast1.add(setDatails);
}
return videoLiast1;
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (Throwable throwable) {
throwable.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (myReader != null) {
myReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected void onPostExecute(List<SetDatails> videoList) {
super.onPostExecute(videoList);
if(videoList!=null) {
try {
MyListAdapter myListAdapter = new MyListAdapter(context1, videoList);
listView1.setAdapter(myListAdapter);
}catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}
I have created an Array adapter to fill list view.
public class MyListAdapter extends ArrayAdapter<SetDatails>{
public static int count =5;
List<SetDatails> videoList;
MyListAdapter(Context context,List<SetDatails> object){
super(context,R.layout.tab2_bookchilds,object);
videoList = object;
}
/**
* {#inheritDoc}
*/
#Override
public int getCount() {
return super.getCount();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater=LayoutInflater.from(getContext());
View view = layoutInflater.inflate(R.layout.tab2_bookchilds, parent, false);
TextView title = (TextView)view.findViewById(R.id.txt_title);
title.setText(videoList.get(position).getTitle());
ImageView imgV_Thumbnail = (ImageView) view.findViewById(R.id.imgV_thumbnail);
Picasso.with(getContext())
.load(videoList.get(position).getThumbnail())
.error(R.drawable.icon_white)
.into(imgV_Thumbnail);
return view;
}
}
I have searched a lot and many people suggest getCount() method although I tried a lot to use this method but don't know how to do that.
Please guys help me because my app is ready to upload but I can't solve this one issue.
I am not able to load the custom listview with my json response. Here is my code.
// my activity
public class LocalExploreActivity extends Activity {
ListView lvEvents;
JSONAdapter adapter;
JSONArray eventResponseArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_explore);
lvEvents = (ListView) findViewById(R.id.eventsList);
adapter = new JSONAdapter (LocalExploreActivity.this,eventResponseArray);
lvEvents.setAdapter(adapter);
}
private void getEventsList() throws JSONException {
JSONObject jsonObjSend = new JSONObject();
JSONObject jsonObjRecv = HttpGetClient.SendHttpPost(URL);
eventResponseArray = jsonObjRecv.getJSONArray("events");
System.out.println("events array issssssssssss "+eventResponseArray);
System.out.println("array len "+eventResponseArray.length());
JSONObject parkObj;
JSONObject activityObj;
JSONArray sessionArray;
for (int i = 0; i < eventResponseArray.length(); i++) {
JSONObject eventDetails = eventResponseArray.getJSONObject(i);
parkObj = eventDetails.getJSONObject("park");
activityObj = eventDetails.getJSONObject("activity");
eventName = eventDetails.getString("name");
System.out.println("the eventName is 33333 "+eventName);
eventFee = eventDetails.getString("fee");
System.out.println("the eventFee is 33333 "+eventFee);
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
EventModel.PREF_EVENTNAME = prefs.getString(eventName, eventName);
EventModel.PREF_EVENTFEE = prefs.getString(eventFee, eventFee);
}
}
}
// my Adapter
public class JSONAdapter extends BaseAdapter implements ListAdapter{
private final Activity activity;
private final JSONArray jsonArray;
protected JSONAdapter (Activity activity, JSONArray jsonArray) {
assert activity != null;
assert jsonArray != null;
this.jsonArray = jsonArray;
this.activity = activity;
}
#Override public int getCount() {
if(null==jsonArray)
return 0;
else
return jsonArray.length();
}
#Override public JSONObject getItem(int position) {
if(null==jsonArray) return null;
else
return jsonArray.optJSONObject(position);
}
#Override public long getItemId(int position) {
JSONObject jsonObject = getItem(position);
return jsonObject.optLong("id");
}
#Override public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = activity.getLayoutInflater().inflate(R.layout.custom_events_list, null);
TextView txtEventName = (TextView)convertView.findViewById(R.id.eventTitleTV);
TextView txtEventFee = (TextView)convertView.findViewById(R.id.eventPriceTV);
JSONObject json_data = getItem(position);
System.out.println("the json data received is 111111111 "+json_data);
if(null!=json_data ){
String eventName = null;
String eventFee = null;
try {
eventName = json_data.getString("name");
eventFee=json_data.getString("fee");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
txtEventName.setText(eventName);
txtEventFee.setText(eventFee);
}
return convertView;
}
}
When I run the app in the Debug mode, and check for the value in the below code,
adapter = new JSONAdapter (LocalExploreActivity.this,eventResponseArray);
The array is null. Need Help!!
You don't appear to be initializing the eventResponseArray anywhere so of course its null.