3
回答
handle更新UI出现错误
极速云服务器,低至1.04元/天>>>   

想写一个聊天窗口,与服务器进行聊天,发送信息在ui线程中,接收消息在子线程中,现在的问题是接收消息的时候会出现错误,初步判断是这一句有问题:historyTxt.setText(str);  望高手指教,谢谢!

详细错误信息:

04-24 04:38:07.168: D/AndroidRuntime(587): Shutting down VM

04-24 04:38:07.168: W/dalvikvm(587): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

04-24 04:38:07.178: E/AndroidRuntime(587): Uncaught handler: thread main exiting due to uncaught exception

04-24 04:38:07.199: E/AndroidRuntime(587): java.lang.NullPointerException

04-24 04:38:07.199: E/AndroidRuntime(587): at discuss.tap.MyTap$1.handleMessage(MyTap.java:142)

04-24 04:38:07.199: E/AndroidRuntime(587): at android.os.Handler.dispatchMessage(Handler.java:99)

04-24 04:38:07.199: E/AndroidRuntime(587): at android.os.Looper.loop(Looper.java:123)

04-24 04:38:07.199: E/AndroidRuntime(587): at android.app.ActivityThread.main(ActivityThread.java:4363)

04-24 04:38:07.199: E/AndroidRuntime(587): at java.lang.reflect.Method.invokeNative(Native Method)

04-24 04:38:07.199: E/AndroidRuntime(587): at java.lang.reflect.Method.invoke(Method.java:521)

04-24 04:38:07.199: E/AndroidRuntime(587): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

04-24 04:38:07.199: E/AndroidRuntime(587): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

04-24 04:38:07.199: E/AndroidRuntime(587): at dalvik.system.NativeStart.main(Native Method)

04-24 04:38:07.218: I/dalvikvm(587): threadid=7: reacting to signal 3

04-24 04:38:07.218: E/dalvikvm(587): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

源码:

package discuss.tap;


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 android.app.TabActivity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TabHost;

import android.widget.TextView;

 

public class MyTap extends TabActivity {

private Socket socket;

private EditText tab1_inputTxt;

private TextView historyTxt;

private Button tab1_sendBtn;


public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setTitle("ChatWindows");

TabHost tabHost = getTabHost();

LayoutInflater.from(this).inflate(R.layout.main,tabHost.getTabContentView(), true);

tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.tab1));

tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.tab2));

try {

connect2server();

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

findviews();

MyTap d = new MyTap();

GetDataThread  gdt = d.new GetDataThread(socket);

gdt.start();

setonclick();

}

    // 连接服务器

    private void connect2server()

                          throws UnknownHostException, IOException

    {   

    socket = new Socket("59.66.137.19",5002);

    }

 

    public void findviews()

    {

    tab1_inputTxt = (EditText)this.findViewById(R.id.tab1_inputTxt);

    historyTxt = (TextView)this.findViewById(R.id.tab1_historyTxt);

    tab1_sendBtn   = (Button)this.findViewById(R.id.tab1_sendBtn);

    }

    

    private void setonclick()

    {

    tab1_sendBtn.setOnClickListener(new View.OnClickListener() 

        {

   

            public void onClick(View v) 

            {

            try {

sendMsg(tab1_inputTxt.getText().toString());

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

            }

        });   

    }


    // 发送消息进行通信

   public void sendMsg(String socketData) 

                         throws IOException

   {

      historyTxt.setText(historyTxt.getText() +"\n" + "我说:" + socketData);

    tab1_inputTxt.setText("");

   

    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        writer.write(socketData+"\n");

        writer.flush();  

   }

   


    class GetDataThread extends Thread{

    private Socket socket;

    public GetDataThread(Socket socket){

    this.socket = socket;

    }

    public void run(){

    Log.e("here","threadrun");

    String txt;

try {

//while (true)

{

txt = ReceiveMsg(socket);

mHandler.obtainMessage(1,txt).sendToTarget();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    }

   

    // 返回消息

      private String ReceiveMsg(Socket socket) throws IOException

      {

          BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

     

          String txt=reader.readLine();

          return txt;


      }    

    } 

    

    private Handler mHandler = new Handler() {

public void handleMessage (Message msg) {//此方法在ui线程运行

switch(msg.what) {

case 1:

Log.e("new","before set");

String str = "\n" + (String)msg.obj;

Log.e("new",str);

historyTxt.setText(str);  

Log.e("new","case");

break;

case 0:

break;

}

}

};

}


main.xml:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

<!--tab1的布局 -->

<LinearLayout

    android:id="@+id/tab1"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >



<TextView

   android:id="@+id/tab1_historyTxt"

   android:layout_width="fill_parent"

   android:layout_height="370dp"

   android:background="@drawable/background" />



    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" 

        android:background="@color/gray" >


        <EditText

            android:id="@+id/tab1_inputTxt"

            android:layout_width="wrap_content"

            android:layout_height="fill_parent"

            android:ems="10" >


            <requestFocus />

        </EditText>


        <Button

          android:id="@+id/tab1_sendBtn"

        android:layout_width="fill_parent"

          android:layout_height="64dp"

            android:text="Button" />

    </LinearLayout>

     

    </LinearLayout>

    

    <!--tab2的布局 -->

    <LinearLayout

        android:id="@+id/tab2"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >


    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="370dp"

        android:background="@color/white"

        android:orientation="vertical" >

    </LinearLayout>


    <LinearLayout

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:background="@color/gray" >


       <Button

           android:id="@+id/button1"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="铅笔" />


       <Button

           android:id="@+id/button2"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="橡皮" />


       <Spinner

           android:id="@+id/spinner1"

           android:layout_width="67dp"

           android:layout_height="wrap_content"

           android:text="颜色" />

    </LinearLayout>

       

</LinearLayout>

</FrameLayout>

举报
伪书呆子
发帖于6年前 3回/272阅
顶部