我正在开发一个可以通过网络向AV接收器发送命令的Android应用程序。我已经做了一个按钮并对其进行编码,以便在单击按钮时它将命令PWON \ r发送到指定的IP和端口。我还在清单文件中给出了完全的互联网权限。问题是按下按钮后应用程序停止工作。我不确定是什么问题。
CODE:
package av.control.test;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AVControlActivity extends Activity {
Button mbutt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mbutt = (Button)findViewById(R.id.Button);
}
public void onClick(View view) throws UnknownHostException, IOException
{
new Thread() {
public void run() {
Socket socket = null;
try {
socket = new Socket("192.168.1.74", 23);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
dataOutputStream.Object BUFFER = null;
write(BUFFER);
String buffer="PWON\r";
OutputStream os=socket.getOutputStream();
os.write(buffer.getBytes());
} catch(IOException e)
{
//error code
}
}
}.start();
}
private void write(Object BUFFER) {
// TODO Auto-generated method stub
}
}
以下是main.xml,如果有任何用处:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Power On"
android:onClick="onClick" />
</LinearLayout>
我很感激给予任何帮助。
编辑:
logcat的
09-15 14:31:30.742: I/dalvikvm(1209): threadid=3: reacting to signal 3
09-15 14:31:31.301: E/dalvikvm(1209): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
09-15 14:31:31.322: I/dalvikvm(1209): threadid=3: reacting to signal 3
09-15 14:31:31.421: I/dalvikvm(1209): Wrote stack traces to '/data/anr/traces.txt'
09-15 14:31:31.801: I/dalvikvm(1209): threadid=3: reacting to signal 3
09-15 14:31:31.901: I/dalvikvm(1209): Wrote stack traces to '/data/anr/traces.txt'
09-15 14:31:32.381: I/dalvikvm(1209): threadid=3: reacting to signal 3
09-15 14:31:32.451: I/dalvikvm(1209): Wrote stack traces to '/data/anr/traces.txt'
09-15 14:31:32.711: D/gralloc_goldfish(1209): Emulator without GPU emulation detected.
09-15 14:31:32.843: I/dalvikvm(1209): threadid=3: reacting to signal 3
09-15 14:31:32.901: I/dalvikvm(1209): Wrote stack traces to '/data/anr/traces.txt'
09-15 14:34:53.081: W/System.err(1209): java.net.ConnectException: failed to connect to /192.168.1.74 (port 23): connect failed: ETIMEDOUT (Connection timed out)
09-15 14:34:53.091: W/System.err(1209): at libcore.io.IoBridge.connect(IoBridge.java:114)
09-15 14:34:53.091: W/System.err(1209): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-15 14:34:53.101: W/System.err(1209): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
09-15 14:34:53.121: W/System.err(1209): at java.net.Socket.startupSocket(Socket.java:566)
09-15 14:34:53.121: W/System.err(1209): at java.net.Socket.tryAllAddresses(Socket.java:127)
09-15 14:34:53.132: W/System.err(1209): at java.net.Socket.<init>(Socket.java:177)
09-15 14:34:53.132: W/System.err(1209): at java.net.Socket.<init>(Socket.java:149)
09-15 14:34:53.161: W/System.err(1209): at av.control.test.AVControlActivity$1.run(AVControlActivity.java:27)
09-15 14:34:53.161: W/System.err(1209): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
09-15 14:34:53.182: W/System.err(1209): at libcore.io.Posix.connect(Native Method)
09-15 14:34:53.182: W/System.err(1209): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
09-15 14:34:53.191: W/System.err(1209): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-15 14:34:53.211: W/System.err(1209): at libcore.io.IoBridge.connect(IoBridge.java:112)
09-15 14:34:53.211: W/System.err(1209): ... 7 more
09-15 14:34:53.222: W/dalvikvm(1209): threadid=12: thread exiting with uncaught exception (group=0x409c01f8)
09-15 14:34:53.241: E/AndroidRuntime(1209): FATAL EXCEPTION: Thread-121
09-15 14:34:53.241: E/AndroidRuntime(1209): java.lang.NullPointerException
09-15 14:34:53.241: E/AndroidRuntime(1209): at av.control.test.AVControlActivity$1.run(AVControlActivity.java:39)
答案 0 :(得分:1)
上有空指针异常
OutputStream os=socket.getOutputStream();
这是因为下线由于超时而给出了异常。
socket = new Socket("192.168.1.74", 23);
所以socket仍为null。如果没有创建socket,我不认为你应该继续操作。所以改变你的代码如下。
try {
socket = new Socket("192.168.1.74", 23);
dataOutputStream.Object BUFFER = null;
write(BUFFER);
String buffer="PWON\r";
OutputStream os=socket.getOutputStream();
os.write(buffer.getBytes());
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
进一步检查你为何超时。这可能是由于设备不在同一网络上,或端口未打开