- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个应用程序,我希望我的应用程序在启动时显示流行电影海报的网格。电影海报是从 TheMovieDataBase API 下载的。然后我使用 Picasso 加载图像。对于 GridView,我还使用了自定义适配器。我不明白该怎么做。这是我到目前为止所做的
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
:
Collections.addAll(urls, moviePosterPath);
但你还没有在任何地方初始化它。用一些值初始化它,它应该可以解决错误。
关于android - 制作一个从 tmdb.org 接收 Json 数据的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38243817/
我正在尝试将 TMDB 包装器 (com.omertron:themoviedbapi:4.0) 添加到我的 Android 工作室项目中。我通过编辑库和依赖项添加了它。但是当我添加后运行应用程序时,
我有一个网站,使用 TMDB's api 。当用户想要登录时,他会从我的网站重定向到 TMDb 的网站进行登录。我想在他登录后将他重定向到我的网站。我正在使用 javascript 将他重定向到 TM
您好 我正在使用 TMDB Api,但我的代码出现错误,说我只有 29 个结果中的 20 个。 原因是TMDB中的搜索功能没有返回第二页数据。 这是 api 搜索短语(没有我的个人 key ): ht
这是我的 api key :7b5e30851a9285340e78c201c4e4ab99 我正在尝试连接到 TMDB api:这是我的代码: package movieDBapiconnnecti
让我们直说吧。我想构建一个流行且适合 child 的电影应用程序,当数据被喜欢时,它存储在 SQLite 中。所以在最喜欢的布局中,我做了两种布局,流行的和 child 的。为了分发数据,我使用了子句
我调用 TMDb Api按类型获取一系列电影。我想访问的许多属性(例如“vote_count”、“poster_path”和“vote_average”)都返回“nil”。 如果我在浏览器中调用 ap
fragment 代码 //获取电影海报 String urlPOster = null; if (currentMovies.has(
我想要 TMDB 中所需电影的海报。我无法从 TMDB API 获取 JSON 数据。我已发送请求,但收到错误 404“找不到您请求的资源”。访问 TMDB 电影 API 的链接:https://de
TMDB.org最近对他们的 API 进行了更改,删除了浏览他们的数据库的功能。我的 Rails 应用程序曾经使用 tmdb-ruby gem浏览 TMDB 数据库,但此 gem 仅适用于 API 的
我正在我的 iOS 应用程序中从 TMDB 获取热门电影数据。但是我在解析和从中获取有意义的数据时遇到了很大的麻烦。我对 iOS 完全陌生。我在 Windows Phone 中做过类似的事情,在那里我
我在 Swift 中制作一个应用程序,它使用来自 TMDB 的 API .我找到了一个 wrapper为了这。使用 Cocoapods 安装它,现在我正在努力使用这些功能。我认为自述文件已过时。因此,
我正在尝试解析从电影数据库返回的 JSON 数据。我收到一条错误消息,告诉我出现以下错误: Uncaught SyntaxError: Unexpected end of JSON input
我正在尝试使用 themoviedb API 访问电影信息,因为 imdb 似乎是一个失败的原因。 他们使用我用 * 替换的 key ,但据我所知,这应该是我进行简单搜索所需的全部内容 所以我的代码很
我正在尝试制作一个应用程序,我希望我的应用程序在启动时显示流行电影海报的网格。电影海报是从 TheMovieDataBase API 下载的。然后我使用 Picasso 加载图像。对于 GridVie
我正在创建我的第一个真正的 React 应用程序,但我已经遇到了一些问题。 我想获取流派列表,但状态停留在“正在加载”。 另外,关于如何根据类型获取数据有什么建议吗?假设如果我单击 Action 类型
我确信你们很多人都使用过 TMDb(电影数据库)API 来观看电影。但我在显示每部电影的类型名称时遇到问题。我正在尝试将来自 movies api 的 genre_ids 中的每个数字替换为来自 ge
如何让用户在 ASP.NET MVC 5 应用程序中使用 TMDB API (The Movie Db) 搜索电影并返回 JSON 结果。 在 VB 外部使用我的个人 api key 并返回包含字符串
我是一名优秀的程序员,十分优秀!