我正在使用此代码,但它会出错,我无法追踪。任何帮助,将不胜感激。这是代码:
package com.example.test;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView display;
private MyDatagramReceiver myDatagramReceiver = null;
protected void onResume() {
myDatagramReceiver = new MyDatagramReceiver();
myDatagramReceiver.start();
}
protected void onPause() {
// myDatagramReceiver.kill();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display=(TextView) findViewById(R.id.textView1);
}
@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;
}
private class MyDatagramReceiver extends Thread {
private boolean bKeepRunning = true;
private String lastMessage = "";
public void run() {
String message;
byte[] lmessage = new byte[100];
try {
DatagramSocket socket = new DatagramSocket(2690);
Log.v("Run", "Socket Created");
while(bKeepRunning) {
DatagramPacket packet = new DatagramPacket(lmessage, lmessage.length);
socket.receive(packet);
message = new String(lmessage, 0, packet.getLength());
lastMessage = message;
runOnUiThread(updateTextMessage);
Log.v("Receiving", "packet recvd");
}
if (socket != null) {
socket.close();
Log.v("SOC", "Socket closed");
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
public void kill() {
bKeepRunning = false;
}
public String getLastMessage() {
return lastMessage;
}
private Runnable updateTextMessage = new Runnable() {
public void run() {
if (myDatagramReceiver == null) return;
display.setText(myDatagramReceiver.getLastMessage());
}
};
}
}
以下是错误:
11-14 03:04:06.240: E/AndroidRuntime(881): FATAL EXCEPTION: main
11-14 03:04:06.240: E/AndroidRuntime(881): Process: com.example.test, PID: 881
11-14 03:04:06.240: E/AndroidRuntime(881): java.lang.RuntimeException: Unable to resume activity {com.example.test/com.example.test.MainActivity}: android.util.SuperNotCalledException: Activity {com.example.test/com.example.test.MainActivity} did not call through to super.onResume()
11-14 03:04:06.240: E/AndroidRuntime(881): java.lang.RuntimeException: Unable to resume activity {com.example.test/com.example.test.MainActivity}: android.util.SuperNotCalledException: Activity {com.example.test/com.example.test.MainActivity} did not call through to super.onResume()
11-14 03:04:06.240: E/AndroidRuntime(881): Caused by: android.util.SuperNotCalledException: Activity {com.example.test/com.example.test.MainActivity} did not call through to super.onResume()
由于
答案 0 :(得分:0)
在代码中添加缺少的超级呼叫:
protected void onResume() {
super.onResume();
myDatagramReceiver = new MyDatagramReceiver();
myDatagramReceiver.start();
}
protected void onPause() {
super.onPause();
// myDatagramReceiver.kill();
}
答案 1 :(得分:0)
问题不在于UDP,“以下是错误”已经说明了错误。如:
MainActivity did not call through to super.onResume()
这意味着,你必须将super.onResume()作为覆盖方法的第一行,这就是你应该做的:
@Override
public void onResume(){
super.onResume();
...
}
除了同样的观点,您可以覆盖其他活动方法。