简体   繁体   English

如何修复无法在尚未调用Looper.prepare()的线程内创建处理程序

[英]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);

    }

Method Call : 方法调用:

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 ? 我该如何解决 ?

Call looper.prepare() in the run() method of your new thread. 在新线程的run()方法中调用looper.prepare()

A Handler can't dispatch Messages or Runnables to the Looper of a thread if it does not exist. 一个Handler无法调度MessagesRunnablesLooper线程,如果它不存在。 Refer to this in the official documentation. 请参阅官方文档。

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.

相关问题 Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() 无法在警告对话框线程中未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() on alert dialog thread 无法在未在AsyncTask for ProgressDialog中调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog java.lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() java.lang.RuntimeException:无法在尚未调用 Looper.prepare() 的线程内创建处理程序错误 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() Error 无法在 AsyncTask 中未调用 Looper.prepare() 的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() in AsyncTask 无法在未调用Looper.prepare()Android的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() Android java.lang.RuntimeException:无法在未在Android中调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() in Android RxJava无法在未调用Looper.prepare()的线程内创建处理程序 - RxJava Can't create handler inside thread that has not called Looper.prepare() 线程化-无法在未调用Looper.prepare()的线程内创建处理程序 - Threading - Can't create handler inside thread that has not called Looper.prepare()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM