Puja Garg February 2016

java.lang.RuntimeException: An error occured while executing doInBackground() in Popmovie app

I am beginner in android development . I am trying to make movies app but I got this error . Here is my code

public class MovieFragment extends Fragment {

    public MovieFragment() {
    }
  //  ArrayAdapter<String> movieAdapter;
    MovieAdapter movieAdapter;
    GridView gridView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Add this line in order for this fragment to handle menu events.
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.moviefragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_refresh) {
            FetchMovieTask movieTask = new FetchMovieTask();
            movieTask.execute();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

     /*   movieAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.gridview_item,
                R.id.item_imageView,
                new ArrayList<String>()); */
        movieAdapter = new MovieAdapter(getActivity());
        gridView = (GridView)rootView.findViewById(R.id.gridview); 

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                 

Answers


Yazan February 2016

there is an uninitialized arraylist movies in method getMovieDataFromJson()

so when you call movies.add(indexMovie); it cause NPE, to solve this change:

ArrayList<Movie> movies = null;

to this

ArrayList<Movie> movies = new ArrayList<>();

in method getMovieDataFromJson()

Post Status

Asked in February 2016
Viewed 2,910 times
Voted 11
Answered 1 times

Search




Leave an answer