2012-01-24 95 views
0

我最近一直在构建一个Android应用程序,并且我在网络部分遇到了一些问题。首先,我得到了this错误,现在我升级了程序,但现在我又遇到了另一个问题。我有一个单独的类,它启动它自己的线程用于发送,另一个用于接收。下面是一个用于发送:当通过Android中的MulticastSocket接收数据时出现错误

import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.InetAddress; 
import java.net.MulticastSocket; 

import android.widget.EditText; 

public class Sender implements Runnable { 

    private MulticastSocket so; 
    private InetAddress serverAddress; 
    private int port; 
    private EditText messageBoard; 
    private EditText eText1; 
    private EditText eText2; 
    private EditText eText3; 
    private Thread myActivity = new Thread(this); 

    public Sender(EditText etHost, EditText etPort, EditText messageBoard, EditText etSend) { 
     eText1 = etHost; 
     eText2 = etPort; 
     eText3 = etSend; 
     this.messageBoard = messageBoard; 
     myActivity.start(); 
    } 

    @Override 
    public void run() { 

     // convert the host name to InetAddress 
     try { 
      //serverAddress = InetAddress.getByName(eText1.getText().toString()); 
      serverAddress = InetAddress.getByName("atlas.dsv.su.se"); 
     } catch (Exception e) {} 

     //convert the port to an int 
     //port = Integer.parseInt(eText2.getText().toString()); 
     port = 4456; 

     // create socket and start communicating 
     try { 
      so = new MulticastSocket(port); 
      so.joinGroup(serverAddress); 
     } catch (IOException e) {} 

     // start listening for incoming messages 
     new Receiver(so, messageBoard); 
    } 

    /** 
    * This method copies the text from the input text field to the conversation text field and 
    * cleans the input text field 
    */ 
    public void sendMessage() { 

     // get the text that they contain and add the new messages to the old ones 
     String message = eText3.getText().toString(); 
     String conversation = messageBoard.getText().toString(); 
     String newConverstion = conversation.concat("\n[You] ").concat(message); 

     // make the messages text view editable 
     messageBoard.setFocusable(true); 
     messageBoard.setText(newConverstion); // add the new message to the text view 
     messageBoard.setFocusable(false); // make the messages text view not editable 

     // erase the text on the second text view that has just been sent 
     eText3.setText(""); 

     // Send message to server 

     // convert message to bytes array 
     byte[] data = (message).getBytes(); 

     // create and send a datagram 
     DatagramPacket packet = new DatagramPacket(data, data.length, serverAddress, port); 

     try { 
      so.send(packet); 
     } catch (IOException e) {} 


    } // end of sendMessage 

} 

这里是一个用于接收:

import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.MulticastSocket; 

import android.widget.EditText; 

public class Receiver implements Runnable { 

    private Thread myActivity = new Thread(this); 
    private MulticastSocket so; 
    private EditText messageBoard; 

    public Receiver(MulticastSocket sock, EditText messBo) { 
     so = sock; 
     messageBoard = messBo; 
     myActivity.start(); 
    } 

    @Override 
    public void run() { 
     byte[] data = new byte[1024]; // received data container 

     while (true) { 
      try { 
       // create a datagram for receiving 
       DatagramPacket packet = new DatagramPacket(data, data.length); 

       // wait for the next message 
       so.receive(packet); 

       String message = new String(data, 0, packet.getLength()); 

       // add the new messages to the old ones 
       String conversation = messageBoard.getText().toString(); 
       String newConverstion = conversation.concat("\n[Remote] ").concat(message); 

       // make the messages text view editable 
       messageBoard.setFocusable(true); 
       messageBoard.setText(newConverstion); // add the new message to the text view 
       messageBoard.setFocusable(false); // make the messages text view not editable 


      } catch (IOException ioe) {} 
     } 
    } 

} 

当我运行它,我得到:

01-25 00:23:27.281: W/dalvikvm(582): threadid=12: thread exiting with uncaught exception (group=0x409c01f8) 
01-25 00:23:27.281: E/AndroidRuntime(582): FATAL EXCEPTION: Thread-80 
01-25 00:23:27.281: E/AndroidRuntime(582): java.lang.NullPointerException 
01-25 00:23:27.281: E/AndroidRuntime(582): at com.regeduser00x.proj1.Receiver.run(Receiver.java:31) 
01-25 00:23:27.281: E/AndroidRuntime(582): at java.lang.Thread.run(Thread.java:856) 

和线路31 so.receive(packet);是什么它的问题?

+0

在'发件人。运行'你有一对'try' /'catch'对,但是如果有一个异常你不做任何事情并且让func没有任何事情发生,继续下去如果'try'中的代码失败,这可能会导致'null'引用。 –

回答

0

我最好的猜测是你的套接字进入你的接收器构造函数null。但没有看到你如何试图使用这个,我不能肯定地说。

我可以说,尽管你可能会发现使用AsyncTask实现类更简单。它们是专门为创建线程和与线程交互而创建的。

以下是一些可以帮助您实现目标的方法。

这两个是显影剂文档的一部分:

http://developer.android.com/resources/articles/painless-threading.html

http://developer.android.com/reference/android/os/AsyncTask.html

并且这具有使用的AsyncTask的一个很好的例子:

http://www.screaming-penguin.com/node/7746

+0

看起来很有趣,但我没有得到参数的那部分。如果我无法将接口组件作为类型“AsyncTask”类型传递,如何从接口读取信息并将其发送回接口? – RegedUser00x

+0

其实我停用Receiver,现在它在我尝试发送时崩溃。所以有些东西阻止我发送和接收,虽然我有这个在不同的线程。解决方案可能是使用AsyncTask。有人会给出一个简单的例子,说明如何用AsyncTask重写这些内容? – RegedUser00x

相关问题