Answer the question
In order to leave comments, you need to log in
The application crashes on startup, what could be the reason?
packageinfo.androidhive.slidingmenu;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import info.androidhive.slidingmenu.adapter.CustomListAdapter;
import info.androidhive.slidingmenu.app.AppController;
import info.androidhive.slidingmenu.model.Movie;
public class HomeFragment extends Fragment {
private static final String TAG = HomeFragment.class.getSimpleName();
private static final String url = " api.androidhive.info/json/movies.json ";
private ProgressDialog pDialog;
private List movieList = new ArrayList();
private ListView listView;
private CustomListAdapter volleyAdapter;
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
pDialog = ProgressDialog.show(getActivity(), null, "Loading data...", true);
listView = (ListView) rootView.findViewById(R.id.list);
volleyAdapter = new CustomListAdapter(getActivity(), movieList);
listView.setAdapter(volleyAdapter);
RequestQueue queue = Volley.newRequestQueue(getActivity());
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
pDialog.hide();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList genre = new ArrayList();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
volleyAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
});
AppController.getInstance().addToRequestQueue(movieReq);
return rootView;
}
}
Here is the code in the "News" tab of the menu. The app just crashes on startup. Doesn't give any errors. What could be the problem?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question