java-无法在某些设备上尚未调用Looper.prepare()的线程内创建处理程序

java-无法在某些设备上尚未调用Looper.prepare()的线程内创建处理程序,第1张

概述我知道已经有很多这样的问题,但是我看不出我做错了什么.该应用程序崩溃,但未显示任何内容.另外,该错误不会在我的设备或仿真器上发生.仅在某些设备上(例如30-40%?).无法在未调用Looper.prepare()的线程内创建处理程序MainActivity.javapublicclassMainActivityextendsActivit

我知道已经有很多这样的问题,但是我看不出我做错了什么.该应用程序崩溃,但未显示任何内容.另外,该错误不会在我的设备或仿真器上发生.仅在某些设备上(例如30-40%?).

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

MainActivity.java

public class MainActivity extends Activity implements Runnable {Gebruiker gebruiker = new Gebruiker();private DatabaseHelper db;Context context;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    context = this;    if(isOnline()){        downloadData();    } else {        AlertDialog.Builder builder = new AlertDialog.Builder(context);        builder.setMessage("Deze app vereist een actIEve verbinding met het internet!")               .setCancelable(false)               .setPositivebutton("OK", new DialogInterface.OnClickListener() {                   public voID onClick(DialogInterface dialog, int ID) {                       androID.os.Process.killProcess(androID.os.Process.myPID());                   }               });        AlertDialog alert = builder.create();        alert.show();    }           db = new DatabaseHelper(this);}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.main, menu);    return true;}private ProgressDialog pDialog;public voID downloadData() {    pDialog = ProgressDialog.show(this,            "Controleren op bestaande gebruiker..", "Even geduld", true,            false);    Thread thread = new Thread(this);    thread.start();}public voID run() {    // add downloading code here    httpReader httpReader = new httpReader();    String url = "*****";    url += Secure.getString(getApplicationContext().getContentResolver(),            Secure.ANDROID_ID);    JsonHelper JsonHelper = new JsonHelper();    gebruiker = JsonHelper.getGebruiker((httpReader.getTextFromUrl(url)            .trim()));    handler.sendEmptyMessage(0);}private Handler handler = new Handler() {    @OverrIDe    public voID handleMessage(Message msg) {        pDialog.dismiss();        if (gebruiker == null) {            // ga naar aanmaken gebruiker            Intent intent = new Intent(MainActivity.this,                    AanmakenUser.class);            startActivity(intent);        } else {            // ga naar hoofdmenu            db.droppen();            db.insertGebruiker(gebruiker);            Intent intent = new Intent(MainActivity.this, Hoofdmenu.class);            startActivity(intent);            // ga naar menu        }    }};public boolean isOnline() {    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);    NetworkInfo netInfo = cm.getActiveNetworkInfo();    if (netInfo != null && netInfo.isConnectedOrConnecting()) {        return true;    }    return false;}

logcat的

01-10 16:12:16.444: E/AndroIDRuntime(6090): FATAL EXCEPTION: Thread-1001-10 16:12:16.444: E/AndroIDRuntime(6090): java.lang.ExceptionInInitializerError01-10 16:12:16.444: E/AndroIDRuntime(6090):     at db.jochen.quizapp.MainActivity.run(MainActivity.java:68)01-10 16:12:16.444: E/AndroIDRuntime(6090):     at java.lang.Thread.run(Thread.java:1019)01-10 16:12:16.444: E/AndroIDRuntime(6090): Caused by: java.lang.RuntimeException: Can't create handler insIDe thread that has not called Looper.prepare()01-10 16:12:16.444: E/AndroIDRuntime(6090):     at androID.os.Handler.<init>(Handler.java:121)01-10 16:12:16.444: E/AndroIDRuntime(6090):     at androID.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)01-10 16:12:16.444: E/AndroIDRuntime(6090):     at androID.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)01-10 16:12:16.444: E/AndroIDRuntime(6090):     at androID.os.AsyncTask.<clinit>(AsyncTask.java:152)01-10 16:12:16.444: E/AndroIDRuntime(6090):     ... 2 more01-10 16:12:16.454: D/dalvikvm(6090): GC_CONCURRENT freed 79K, 48% free 2843K/5379K, external 3695K/4614K, paused 2ms+5ms01-10 16:12:16.454: W/ActivityManager(278):   Force finishing activity db.jochen.quizapp/.MainActivity01-10 16:12:16.544: E/SkLayout_wtle(6090): ellipsis Failed line number does not match 1 001-10 16:12:16.624: D/fast-dormancy(165): [FDM]: finish the match exclude List procedure, but can not match any one01-10 16:12:16.624: D/fast-dormancy(165): [FDM]: Godormant? 001-10 16:12:16.624: D/fast-dormancy(165): [FDM]: Threads Status Mutex Locked in fdm_main_loop01-10 16:12:16.624: D/fast-dormancy(165): [FDM]: Threads Status Mutex Unlocked in fdm_main_loop01-10 16:12:16.624: D/fast-dormancy(165): [FDM]: finish the match exclude List procedure, but can not match any one01-10 16:12:16.624: D/fast-dormancy(165): [FDM]: Godormant? 001-10 16:12:16.624: D/fast-dormancy(165): [FDM]: Threads Status Mutex Locked in fdm_main_loop01-10 16:12:16.624: D/fast-dormancy(165): [FDM]: Threads Status Mutex Unlocked in fdm_main_loop01-10 16:12:16.624: I/ActivityManager(278): No longer want com.sonyeriCSSon.tvlauncher (pID 4961): hIDden #1601-10 16:12:16.664: E/WindowManager(6090): Activity db.jochen.quizapp.MainActivity has leaked window com.androID.internal.policy.impl.PhoneWindow$DecorVIEw@2afd4aa0 that was originally added here01-10 16:12:16.664: E/WindowManager(6090): androID.vIEw.WindowLeaked: Activity db.jochen.quizapp.MainActivity has leaked window com.androID.internal.policy.impl.PhoneWindow$DecorVIEw@2afd4aa0 that was originally added here01-10 16:12:16.664: E/WindowManager(6090):      at androID.vIEw.VIEwRoot.<init>(VIEwRoot.java:269)01-10 16:12:16.664: E/WindowManager(6090):      at androID.vIEw.WindowManagerImpl.addVIEw(WindowManagerImpl.java:148)01-10 16:12:16.664: E/WindowManager(6090):      at androID.vIEw.WindowManagerImpl.addVIEw(WindowManagerImpl.java:91)01-10 16:12:16.664: E/WindowManager(6090):      at androID.vIEw.Window$LocalWindowManager.addVIEw(Window.java:424)01-10 16:12:16.664: E/WindowManager(6090):      at androID.app.Dialog.show(Dialog.java:241)01-10 16:12:16.664: E/WindowManager(6090):      at androID.app.ProgressDialog.show(ProgressDialog.java:109)01-10 16:12:16.664: E/WindowManager(6090):      at androID.app.ProgressDialog.show(ProgressDialog.java:97)01-10 16:12:16.664: E/WindowManager(6090):      at db.jochen.quizapp.MainActivity.downloadData(MainActivity.java:59)01-10 16:12:16.664: E/WindowManager(6090):      at db.jochen.quizapp.MainActivity.onCreate(MainActivity.java:33)01-10 16:12:16.664: E/WindowManager(6090):      at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)01-10 16:12:16.664: E/WindowManager(6090):      at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:1623)01-10 16:12:16.664: E/WindowManager(6090):      at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)01-10 16:12:16.664: E/WindowManager(6090):      at androID.app.ActivityThread.access00(ActivityThread.java:121)01-10 16:12:16.664: E/WindowManager(6090):      at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:943)01-10 16:12:16.664: E/WindowManager(6090):      at androID.os.Handler.dispatchMessage(Handler.java:99)01-10 16:12:16.664: E/WindowManager(6090):      at androID.os.Looper.loop(Looper.java:138)01-10 16:12:16.664: E/WindowManager(6090):      at androID.app.ActivityThread.main(ActivityThread.java:3701)01-10 16:12:16.664: E/WindowManager(6090):      at java.lang.reflect.Method.invokeNative(Native Method)01-10 16:12:16.664: E/WindowManager(6090):      at java.lang.reflect.Method.invoke(Method.java:507)01-10 16:12:16.664: E/WindowManager(6090):      at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)01-10 16:12:16.664: E/WindowManager(6090):      at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:636)01-10 16:12:16.664: E/WindowManager(6090):      at dalvik.system.NativeStart.main(Native Method)

解决方法:

如果您的线程不在监听循环程序,您如何管理发送给处理程序的请求?

Doc说:
公共处理程序()

在API级别1中添加
默认构造函数将此处理程序与当前线程的Looper关联.如果此线程没有循环程序,则此处理程序将无法接收消息,因此将引发异常.

如果希望辅助线程能够管理Handler,则必须调用Looper.prepare()

例:

public class MyThread extends Thread {    private Handler mHandler;    public voID run() {        Looper.prepare();        mHandler = new Handler() {            @OverrIDe            public voID handleMessage(Message msg) {                switch (msg.what) {                    // manage the message                }            }        };        Looper.loop();    }    public voID stopLooper() {        if (Looper.myLooper()!=null)            Looper.myLooper().quitSafely();    }}
总结

以上是内存溢出为你收集整理的java-无法在某些设备上尚未调用Looper.prepare()的线程内创建处理程序全部内容,希望文章能够帮你解决java-无法在某些设备上尚未调用Looper.prepare()的线程内创建处理程序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1093500.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存