2016-07-07 7 views
1

私は、起動時に人気の映画ポスターのグリッドを表示するアプリケーションを作成しようとしています。ムービーポスターは、TheMovieDataBase APIからダウンロードされます。私はピカソを使ってイメージを読み込みます。 GridViewではカスタムアダプタも使用しています。私はそれをする方法を理解することができません。ここで私は今tmdb.orgからJsonデータを受信するアプリケーションを作成する

 MovieFragment.java 

    import android.content.Context; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
     import android.support.v4.app.Fragment; 
     import android.util.Log; 
     import android.view.LayoutInflater; 
     import android.view.Menu; 
     import android.view.MenuInflater; 
     import android.view.MenuItem; 
     import android.view.View; 
     import android.view.ViewGroup; 
     import android.widget.BaseAdapter; 
     import android.widget.GridView; 
     import android.widget.ImageView; 

     import com.squareup.picasso.Picasso; 

     import org.json.JSONArray; 
     import org.json.JSONException; 
     import org.json.JSONObject; 

     import java.io.BufferedReader; 
     import java.io.IOException; 
     import java.io.InputStream; 
     import java.io.InputStreamReader; 
     import java.net.HttpURLConnection; 
      import java.net.URL; 
     import java.util.ArrayList; 
     import java.util.Collections; 
     import java.util.List; 


     public class MovieFragment extends Fragment { 
     //ArrayAdapter<String> mMovieAdapter; 
     String[]movieId,movieTitle,movieOverview, 
     movieReleaseDate, 
     moviePosterPath,movieVoteAverage; 
     public MovieFragment() { 
     } 

     MovieAdapter mMovieAdapter; 

      @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup  container, 
         Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

    mMovieAdapter = new MovieAdapter(getActivity()); 
    GridView listView = (GridView) rootView.findViewById(R.id.gridView); 
    listView.setAdapter(mMovieAdapter); 
    updateMovie(); 
    return rootView; 
} 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    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.menu_fragment, menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.action_refresh) { 
     updateMovie(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

private void updateMovie() { 
    FetchMovieTask movieTask = new FetchMovieTask(); 
    movieTask.execute(); 
} 

class FetchMovieTask extends AsyncTask<Void, Void, List<String>> { 
    private final String LOG_TAG = FetchMovieTask.class.getSimpleName(); 

    @Override 
    protected List<String> doInBackground(Void... params) { 
     HttpURLConnection urlConnection = null; 
     BufferedReader reader = null; 

     // Will contain the raw JSON response as a string. 
     String movieJsonStr = null; 

     try { 
      URL url = new URL("http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=c20129fdf73b5df3ab44548ad7f73586"); 

      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.connect(); 

      // Read the input stream into a String 
      InputStream inputStream = urlConnection.getInputStream(); 
      StringBuffer buffer = new StringBuffer(); 
      if (inputStream == null) { 
       // Nothing to do. 
       return null; 
      } 
      reader = new BufferedReader(new InputStreamReader(inputStream)); 

      String line; 
      while ((line = reader.readLine()) != null) { 

       buffer.append(line + "\n"); 
      } 

      if (buffer.length() == 0) { 
       // Stream was empty. No point in parsing. 
       return null; 
      } 
      movieJsonStr = buffer.toString(); 

     } catch (IOException e) { 
      Log.e(LOG_TAG, "Error ", e); 
      return null; 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
      if (reader != null) { 
       try { 
        reader.close(); 
       } catch (final IOException e) { 
        Log.e(LOG_TAG, "Error closing stream", e); 
       } 
      } 
     } 
     try { 
      return getMovieDataFromJson(movieJsonStr); 
     } catch (JSONException j) { 
      Log.e(LOG_TAG, "JSON Error", j); 
     } 
     return null; 
    } 

    private List<String> getMovieDataFromJson(String forecastJsonStr) 
      throws JSONException { 
     JSONObject movieJson = new JSONObject(forecastJsonStr); 
     JSONArray movieArray = movieJson.getJSONArray("results"); 
     List<String> urls = new ArrayList<>(); 
     for (int i = 0; i < movieArray.length(); i++) { 
      JSONObject movie = movieArray.getJSONObject(i); 
      urls.add("http://image.tmdb.org/t/p/w185" + movie.getString("poster_path")); 
     } 
     return urls; 
    } 

    @Override 
    protected void onPostExecute(List<String> strings) { 
     mMovieAdapter.replace(strings); 
    } 
} 

class MovieAdapter extends BaseAdapter { 
    private final String LOG_TAG = MovieAdapter.class.getSimpleName(); 
    private final Context context; 
    private final List<String> urls = new ArrayList<String>(); 

    public MovieAdapter(Context context) { 
     this.context = context; 
     Collections.addAll(urls, moviePosterPath); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = new ImageView(context); 
     } 
     ImageView imageView = (ImageView) convertView; 


     String url = getItem(position); 

     Log.e(LOG_TAG," URL "+url); 

     Picasso.with(context).load(url).into(imageView); 

     return convertView; 
    } 

    @Override 
    public int getCount() { 
     return urls.size(); 
    } 

    @Override 
    public String getItem(int position) { 
     return urls.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 
    public void replace(List<String> urls) { 
     this.urls.clear(); 
     this.urls.addAll(urls); 
     notifyDataSetChanged(); 
    } 
} 

}

私はこのコードを実行すると、それはNullPointerExceptionが表示され、RuntimeExceptionが

 java.lang.RuntimeException: Unable to start activity ComponentInfo 
     {com.codesetters.verader/com.codesetters.verader.MainActivity}:   java.lang.NullPointerException                   atandroid.app.ActivityThreadperformLaunchActivity(ActivityThread.java:2404) 




     Caused by: java.lang.NullPointerException                    atjava.util.Collections.addAll(Collections.java:2582) 

がどのように私はそれらを解決することができますまでアップやっているのですか? あなたがここにmoviePosterPathを使用しましたので、あなたはこのエラーを取得している

答えて

1

助けてください:

Collections.addAll(urls, moviePosterPath); 

しかし、あなたはそれをどこにも初期化されていませんでした。それを何らかの値で初期化し、エラーを解決するはずです。

+0

私はこれを行うことができますこの方法..String [] moviePosterPath = null; –

+0

これはまだ 'null'ですね、これを次のように初期化する必要があります:' string [] moviePosterPath = new String [] {}; 'btw' moviePosterPath'の使い方は分かりませんか? –

0

文字列配列moviePosterPathが初期化されていないため、NULLポインタが存在する可能性があります。 Collections.addAll

これは、配列を初期化する方法です。とにかくArrayListを使用することをお勧めします

String[] moviePosterPath = new String[N]; 
関連する問題