Listview致命异常:从doInBackground内部调用时,AsyncTask#1

时间:2013-01-08 23:41:34

标签: java android android-widget android-listview android-asynctask

我通过客户端/服务器进行简单的聊天,当我将消息发送到服务器时,它完美地显示了ListView Bubble。但是当我从服务器发送一个并且客户端收到它时,它会尝试执行addItems()并关闭它。我尝试了一切。我知道这是因为我无法从Asynctask更改UI,但我是Android / Java的新手,我不知道DOOOOOOOO是什么!帮助PAYASEEEE !!!!!

***MY LOG CAT** 01-08 18:32:41.023: E/AndroidRuntime(1242): FATAL EXCEPTION: AsyncTask #1
01-08 18:32:41.023: E/AndroidRuntime(1242): java.lang.RuntimeException: An error occured while executing doInBackground()
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at java.lang.Thread.run(Thread.java:856)
01-08 18:32:41.023: E/AndroidRuntime(1242): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.view.View.requestLayout(View.java:15129)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.view.View.requestLayout(View.java:15129)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.view.View.requestLayout(View.java:15129)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.view.View.requestLayout(View.java:15129)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.view.View.requestLayout(View.java:15129)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.widget.AbsListView.requestLayout(AbsListView.java:1932)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:813)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:6051)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:286)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.widget.ArrayAdapter.add(ArrayAdapter.java:182)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at com.i911.emergency.response.DiscussArrayAdapter.add(DiscussArrayAdapter.java:26)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at com.i911.emergency.response.HelloBubblesActivity.addItems(HelloBubblesActivity.java:148)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at com.i911.emergency.response.HelloBubblesActivity.access$0(HelloBubblesActivity.java:147)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at com.i911.emergency.response.HelloBubblesActivity$TcpClientTask.doInBackground(HelloBubblesActivity.java:116)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at com.i911.emergency.response.HelloBubblesActivity$TcpClientTask.doInBackground(HelloBubblesActivity.java:1)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-08 18:32:41.023: E/AndroidRuntime(1242):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-08 18:32:41.023: E/AndroidRuntime(1242):     ... 5 more

这是我的代码:

  package com.ChatWithMe;

  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
        import java.net.Socket;
  import java.net.UnknownHostException;
  import com.ChatWithMe.OneComment;
  import android.app.Activity;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.KeyEvent;
  import android.view.View;
  import android.view.View.OnKeyListener;
  import android.widget.EditText;
  import android.widget.ListView;
  import android.widget.TextView;

  public class HelloBubblesActivity extends Activity {
private com.ChatWithMe.DiscussArrayAdapter adapter;
private ListView lv;
private EditText editText1;
public Socket s;
public BufferedWriter out;
public BufferedReader in;
public TextView i911Log;
public EditText DataToSend;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_discuss);
    lv = (ListView) findViewById(R.id.listView1);
    adapter = new DiscussArrayAdapter(getApplicationContext(), R.layout.listitem_discuss);
    lv.setAdapter(adapter);
    new TcpClientTask().execute();

    editText1 = (EditText) findViewById(R.id.editText1);
    editText1.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                // Perform action on key press
                adapter.add(new OneComment(false, "User: " + editText1.getText().toString()));
                adapter.add(new OneComment(true, "User: " + editText1.getText().toString()));
                try {
                out.write("TXT:" + editText1.getText().toString());
                out.flush();
                } catch (UnknownHostException e) {
                     e.printStackTrace();
                     adapter.add(new OneComment(false, "Error Sending: " + editText1.getText().toString()));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    adapter.add(new OneComment(false, "Error Sending: " + editText1.getText().toString()));
                    Log.i("TcpClient", "Button Clicked, but IOException Occured");
                }

                editText1.setText("");
                return true;
            }
            return false;
        }
    });

}

class TcpClientTask extends AsyncTask<Void, Void, Void> {
    private static final int TCP_SERVER_PORT = 1234;
    private boolean error = false;
    Boolean SocketStarted = false;
    private BufferedReader in;
    protected Void doInBackground(Void... arg0) {
        try {
            Socket s = new Socket("10.0.2.2", TCP_SERVER_PORT);
            //10.0.2.2
                in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

            //send output msg
            String outMsg = "VER:Android,ID:1038263,SND:1,VDO:1,GPS:1,";
            out.write(outMsg);
            out.flush();
            String outMsg2 = com.ChatWithMe.MainActivity.GPSTracker.toString();
            out.write(outMsg2);
            out.flush();
            Log.i("TcpClient", "sent: " + outMsg);

            String WhatsIN = in.readLine();
            while(WhatsIN!=null){
            String inMsg = in.readLine() + System.getProperty("line.separator");
            Log.i("TcpClient", "received: " + inMsg);


            String inMsg3 = inMsg.substring(1,inMsg.length() - 1);
            Log.i("Info", "info: " + inMsg3);

            if (inMsg3.equals("GPSX")) {
                String GPSTracking = com.ChatWithMe.MainActivity.GPSTracker.toString();
                Log.i("GPSX", "Sent:" + GPSTracking.toString());
                out.write(GPSTracking);
                out.flush();
            }

            // Check Commands
            String CmdCheck = inMsg3.substring(0,4);
            if (CmdCheck.equals("TXT:")) {
           String SpitItOut = inMsg3.substring(4, inMsg3.length() - 1); 
           Log.i("SpitItOut", "Msg:" + SpitItOut.toString());

          //ERROR IS HERE
           addItems();
          //ERROR IS HERE   
            }

            } 

        } catch (UnknownHostException e) {
            error = true;
            e.printStackTrace();
        } catch (IOException e) {
            error = true;
            e.printStackTrace();
        } 

        return null;
    }

    protected void onPostExecute() {
        if(error) {
            // Something bad happened

        }
        else {
            // Success

        }
    }


}

private void addItems() {
    adapter.add(new OneComment(true, "Hello bubbles!"));
}

}

2 个答案:

答案 0 :(得分:6)

addItems()移至onPostExecute()。您无法修改后台线程中与UI相关的任何内容。

答案 1 :(得分:2)

了解异步任务here。基本上,你有onPostExecute()方法在UI线程上运行,你应该将结果从doInBackground()传递给它,并修改UI或你在主线程中必须做的任何事情。因此,您尝试从后台线程更改适配器时,addItems()方法会产生错误。将其移至onPostExecute(),它可以解决您的问题。