[英]how to fix Can't create handler inside thread that has not called Looper.prepare()
Why am I getting : Can't create handler inside thread that has not called Looper.prepare()
error here ? 我为什么得到:
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() ;
As other answers for similar questions have suggessted , I am not doing any UI updates inside the background method . 由于提出了类似问题的其他答案,因此我没有在background方法内部进行任何UI更新。 But still am getting the error.
但是仍然出现错误。
I'm new to android and java . 我是android和java的新手。 How do I fix this ?
我该如何解决 ?
new Handler(Looper.getMainLooper())
文档中有更多详细信息。
onPostExecute() is invoked on the main UI thread. onPostExecute()在主UI线程上调用。 Try removing the runOnUiThread() call.
尝试删除runOnUiThread()调用。 For more details on AsyncTask see https://developer.android.com/reference/android/os/AsyncTask
有关AsyncTask的更多详细信息,请参见https://developer.android.com/reference/android/os/AsyncTask
The culprit may be one of the method calls in your doInBackground
method. 罪魁祸首可能是
doInBackground
方法中的方法调用之一。 try running those on UIThread: 尝试在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.