[英]Android thread error - Can't create handler inside thread that has not called Looper.prepare()
[英]how to fix Can't create handler inside thread that has not called Looper.prepare()
我为什么得到: Can't create handler inside thread that has not called Looper.prepare()
错误的Can't create handler inside thread that has not called Looper.prepare()
?
public class MovieInterface extends AsyncTask<String, Void, MovieResultsPage> {
public Activity activity;
MovieInterface(Activity context) {
this.activity = context;
}
public void handleUpcomingMovies() {
}
@Override
protected MovieResultsPage doInBackground(String... strings) {
TmdbMovies movies = new TmdbApi("d2e5d02fe295efc00bad8da4dc384edf").getMovies();
MovieResultsPage upcomingMovies = movies.getUpcoming(null, 1, "IN");
int totalpages = upcomingMovies.getTotalPages();
return upcomingMovies;
}
@Override
protected void onPostExecute(final MovieResultsPage movieDbs) {
super.onPostExecute(movieDbs);
ArrayList<MovieClass> mymovielist = new ArrayList<>();
for (MovieDb movieResult : movieDbs.getResults()) {
mymovielist.add(new MovieClass(movieResult.getTitle(), movieResult.getOverview()));
}
MovieAdapter movieClassArrayAdapter = new MovieAdapter(activity, R.layout.movie_list_item, mymovielist);
((ListView) activity.findViewById(R.id.upcomingMoviesList)).setAdapter(movieClassArrayAdapter);
}
MovieInterface movieInterface = new MovieInterface(this) ;
movieInterface.execute() ;
由于提出了类似问题的其他答案,因此我没有在background方法内部进行任何UI更新。 但是仍然出现错误。
我是android和java的新手。 我该如何解决 ?
在新线程的run()
方法中调用looper.prepare()
。
一个Handler
无法调度Messages
或Runnables
的Looper
线程,如果它不存在。 请参阅本官方文档。
new Handler(Looper.getMainLooper())
文档中有更多详细信息。
onPostExecute()在主UI线程上调用。 尝试删除runOnUiThread()调用。 有关AsyncTask的更多详细信息,请参见https://developer.android.com/reference/android/os/AsyncTask
罪魁祸首可能是doInBackground
方法中的方法调用之一。 尝试在UIThread上运行它们:
runOnUiThread(new Runnable() {
@Override
public void run() {
TmdbMovies movies = new TmdbApi("d2e5d02fe295efc00bad8da4dc384edf").getMovies();
MovieResultsPage upcomingMovies = movies.getUpcoming(null, 1, "IN");
int totalpages = upcomingMovies.getTotalPages();
return upcomingMovies;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.