2
我正在嘗試通過重寫onPostExecute方法填充我的GridView來解決問題。 我在onPostExecute方法裏得到一個關於 adapter.add(oneMovie);
的強制關閉。使用自定義適配器填充GridView時遇到困難
我的代碼鏈接如下:
MainActivityFragment.java
public class MainActivityFragment extends Fragment {
MovieAdapter adapter;
public MainActivityFragment() {
}
@Override
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.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_settings) {
MovieDetails movie = new MovieDetails();
movie.execute();
return true;
}
return super.onOptionsItemSelected(item);
}
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String movieinfo = null;
private final String LOG_TAG = MovieDetails.class.getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SingleMovie[] movieList = {};
adapter = new MovieAdapter(getActivity(), Arrays.asList(movieList));
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
GridView gridview = (GridView) rootView.findViewById(R.id.gridView);
gridview.setAdapter(adapter);
return rootView;
}
public class MovieDetails extends AsyncTask<Void, Void, SingleMovie[]> { //Line number 84 according to log
@Override
protected void onPostExecute(SingleMovie[] singleMovie) {
if (singleMovie != null) {
adapter.clear();
for (int i = 0; i < singleMovie.length; i++) {
SingleMovie oneMovie = singleMovie[i];
Log.v(LOG_TAG, oneMovie.movieTitle + oneMovie.movieImage);
adapter.add(oneMovie); //Line number 92 according to log
}
}
super.onPostExecute(singleMovie);
}
private SingleMovie[] getmovieData(String movieInfo)
throws JSONException {
final String MDB_RESULT = "results";
final String MDB_TITLE = "title";
final String MDB_POSTER = "poster_path";
JSONObject moviejson = new JSONObject(movieInfo);
JSONArray movieArray = moviejson.getJSONArray(MDB_RESULT);
String baseURL = "http://image.tmdb.org/t/p/w185/";
SingleMovie[] movieDetails = new SingleMovie[5];
for (int i = 0; i < 5; i++) {
JSONObject currentMovie = movieArray.getJSONObject(i);
String movietitle = currentMovie.getString(MDB_TITLE);
String moviePosterendURL = currentMovie.getString(MDB_POSTER);
String moviePosterURL = baseURL + moviePosterendURL;
movieDetails[i] = new SingleMovie(moviePosterURL, movietitle);
}
return movieDetails;
}
@Override
protected SingleMovie[] doInBackground(Void... params) {
try {
URL url = new URL("http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=APPID");
String movieDbUrl = url.toString();
Log.v(LOG_TAG, movieDbUrl);
// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuilder buffer = new StringBuilder();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
movieinfo = buffer.toString();
Log.v(LOG_TAG, movieinfo);
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}
try {
return getmovieData(movieinfo);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}
MovieAdapter.java
public class MovieAdapter extends ArrayAdapter<SingleMovie>{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SingleMovie singleMoviecontent = getItem(position);
View rootView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
ImageView poster = (ImageView) rootView.findViewById(R.id.movie_poster_image);
Picasso.with(getContext()).load(singleMoviecontent.movieImage).into(poster);
TextView name = (TextView) rootView.findViewById(R.id.movie_name);
name.setText(singleMoviecontent.movieTitle);
return rootView;
}
public MovieAdapter(FragmentActivity context, List<SingleMovie> resource) {
super(context, 0, resource);
}
}
SingleMovie.java
public class SingleMovie {
String movieImage;
String movieTitle;
public SingleMovie(String image, String title){
this.movieImage = image;
this.movieTitle = title;
}
}
代碼的其他部分工作得很好。 我在各個部分記錄了它,並且日誌消息與預期一致。 代碼在adapter.add(oneMovie)處中斷;裏面有postexecute方法。 對此的任何幫助將是偉大的。
由於
日誌消息:
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: FATAL EXCEPTION: main
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: Process: io.github.the_dagger.movies, PID: 1870
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: java.lang.UnsupportedOperationException
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at java.util.AbstractList.add(AbstractList.java:404)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at java.util.AbstractList.add(AbstractList.java:425)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at io.github.the_dagger.movies.MainActivityFragment$MovieDetails.onPostExecute(MainActivityFragment.java:92)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at io.github.the_dagger.movies.MainActivityFragment$MovieDetails.onPostExecute(MainActivityFragment.java:84)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.os.AsyncTask.finish(AsyncTask.java:636)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.os.AsyncTask.access$500(AsyncTask.java:177)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.os.Looper.loop(Looper.java:139)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5308)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
01-07 11:25:09.818 1870-1870/io.github.the_dagger.movies E/AndroidRuntime: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)
歡迎您 – Blackbelt