Android 如何解决致命异常:Thread-11

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12709799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:29:49  来源:igfitidea点击:

How to solve FATAL EXCEPTION: Thread-11

androidmultithreadingfatal-error

提问by NagarjunaReddy

Now i am developing one in that useing Async Task Iam facing this Errors, Any one have idea of this..

现在我正在开发一个使用异步任务的我面临这个错误,任何人都有这个想法..

10-03 19:04:55.662: E/AndroidRuntime(1547): FATAL EXCEPTION: Thread-11
10-03 19:04:55.662: E/AndroidRuntime(1547): java.lang.ExceptionInInitializerError
10-03 19:04:55.662: E/AndroidRuntime(1547):     at com.cc.accounts.ChatAccountsFragment.Gtalk_logInComplete(ChatAccountsFragment.java:579)
10-03 19:04:55.662: E/AndroidRuntime(1547):     at com.cc.xmpp.GmailXmppClient.Login(GmailXmppClient.java:92)
10-03 19:04:55.662: E/AndroidRuntime(1547):     at com.cc.accounts.ChatAccountsFragment.run(ChatAccountsFragment.java:482)
10-03 19:04:55.662: E/AndroidRuntime(1547):     at java.lang.Thread.run(Thread.java:1096)
10-03 19:04:55.662: E/AndroidRuntime(1547): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-03 19:04:55.662: E/AndroidRuntime(1547):     at android.os.Handler.<init>(Handler.java:121)
10-03 19:04:55.662: E/AndroidRuntime(1547):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
10-03 19:04:55.662: E/AndroidRuntime(1547):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
10-03 19:04:55.662: E/AndroidRuntime(1547):     at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
10-03 19:04:55.662: E/AndroidRuntime(1547):     ... 4 more

this is Async Task

这是异步任务

public void Gtalk_logInComplete(String Uname, XMPPConnection _connection) {
      Log.i(TAG, "Uname" +Uname);
      Uname = current_username;
      _connection = current_connection;
      ForwardAction task = new ForwardAction(); // this is line no 579
      task.execute();    
}

private class ForwardAction extends AsyncTask<Void, Void, Void> {          
    private ArrayList<String> contactJids;
    private GmailXmppClient Gtalk_xmpp_client;
    private ArrayList<String> contactNames;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        show();
    }

    protected Void doInBackground(Void... params) {

 //         new Thread() {
 //             public void run() {

                synchronized (this.contactJids) {
                    Roster roster = this.Gtalk_xmpp_client.getRoster();
                    String file_name;
                    for (RosterEntry entry : roster.getEntries()) {
                        if (entry.getType() == ItemType.to || entry.getType() == ItemType.both) {

                            this.contactJids.add(entry.getUser());
                            this.contactNames.add(entry.getName());                         

                            contact_db.open();
                            boolean yes = contact_db.checkUsername(entry.getUser());
                            Log.i(TAG, "Con=" + yes);
                            if (!yes) {

                                Bitmap buddy_img = buddyImage(entry, current_connection);
                                if (buddy_img != null)
                                    file_name = Store(buddy_img);
                                else
                                    file_name = "";
                                if (entry.getName() == null)
                                    contact_db.createContact(entry.getUser(), entry.getUser(), current_username, file_name, "GOOGLE");
                                else
                                    contact_db.createContact(entry.getName(), entry.getUser(), current_username, file_name, "GOOGLE");
                            } else {
                                Log.i(TAG, "Con=exist");
                            }
                        }
                    }
                } 
    //      }
//      }.start();                      
        contact_db.close();         
        return null;
    }

    protected void onPostExecute(Void aVoid) {          
        super.onPostExecute(aVoid);       
        hide();
    }        
}

回答by Nick

This error happens when you try to update a view from a thread which you can't do. You'll either need to update your view from onPostExecuteor use runOnUiThreadto update your view.

当您尝试从无法执行的线程更新视图时,会发生此错误。您要么需要更新视图,onPostExecute要么使用runOnUiThread来更新视图。

You can use runOnUiThread as follows:

您可以按如下方式使用 runOnUiThread:

runOnUiThread(new Runnable() {
    public void run() {                     
        // your code to update the UI thread here               
    }
});