0

我有一个异步 UDP 接收任务侦听数据,然后调用回调函数将接收到的数据字符串返回给主活动。我在 onPostExecute() 函数中得到一个空指针异常。

这是来自 logcat 的确切错误:

10-28 18:14:44.277: E/AndroidRuntime(5208): FATAL EXCEPTION: main
10-28 18:14:44.277: E/AndroidRuntime(5208): java.lang.NullPointerException
10-28 18:14:44.277: E/AndroidRuntime(5208):     at com.ocelot.healthviewer.UdpReceiveTask.onPostExecute(UdpReceiveTask.java:55)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at com.ocelot.healthviewer.UdpReceiveTask.onPostExecute(UdpReceiveTask.java:1)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at android.os.Looper.loop(Looper.java:158)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at android.app.ActivityThread.main(ActivityThread.java:5751)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at java.lang.reflect.Method.invokeNative(Native Method)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at java.lang.reflect.Method.invoke(Method.java:511)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
10-28 18:14:44.277: E/AndroidRuntime(5208):     at dalvik.system.NativeStart.main(Native Method)

界面在这里

public interface OnTaskCompleted {
public void onTaskCompleted();

}

这是UDP接收任务代码

public class UdpReceiveTask extends AsyncTask<Void, Void, String> {

private OnTaskCompleted listener;

int port = 25566;
public boolean stop = false;

DatagramSocket socket;
DatagramPacket packet;
byte[] message_buffer;
String data;
public String output;

@Override
protected void onPreExecute()
{
    message_buffer = new byte[512];

    try {
        socket = new DatagramSocket(port);
    } catch (SocketException e) {
        e.printStackTrace();
    }

    packet = new DatagramPacket(message_buffer, message_buffer.length);
}

@Override
protected String doInBackground(Void... params) {

    try {
        socket.receive(packet);
    } catch (IOException e) {
        e.printStackTrace();
    }

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

    return data;

}

@Override
protected void onPostExecute(String output) {
    output = data;
    listener.onTaskCompleted();

}

}

这是我的主要活动:

public class MainActivity extends Activity implements OnTaskCompleted {

public TextView tv1;
boolean done = false;
UdpReceiveTask receiveTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1 =(TextView)findViewById(R.id.textView1);
    receiveTask = new UdpReceiveTask();

    super.onStart();
}

@Override
public 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;
}

public void StartReceive(View view)
{
    if(done == false)
    {
        receiveTask.execute();
        done = true;
    }

}

@Override
public void onTaskCompleted() {
    String data = receiveTask.output;
    tv1.append(data);

}

}

提前致谢 - 阿德里安

编辑 由于codeMagic,我摆脱了第一个空指针错误,但现在我得到另一个与文本视图有关的空指针异常:

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.TextView.append(TextView.java:3393)
at com.ocelot.healthviewer.MainActivity.onTaskCompleted(MainActivity.java:47)
4

1 回答 1

2

看起来listenernull。尝试添加

receiveTask.listener = this;

receiveTask = new UdpReceiveTask();

在你的Activity

于 2013-10-28T18:33:59.097 回答