简体   繁体   English

Android App停止工作

[英]Android App Stopped working

I'm currently developing an android application which can send commands to an AV Receiver through the network. 我目前正在开发一个android应用程序,可以通过网络将命令发送到AV接收器。 I have gotten as far as making a button and coding it so that when the button is clicked it will send the command PWON\\r to the IP and port specified. 我已经做了一个按钮并进行了编码,以便在单击该按钮时将命令PWON \\ r发送到指定的IP和端口。 I have also given full internet permission in the manifest file. 清单文件中还提供了完整的Internet权限。 The problem is that the app stops working as soon as the button is pressed. 问题是,一旦按下按钮,应用程序就会停止工作。 I'm not sure what the problem is. 我不确定是什么问题。

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

}
}

Here is the main.xml as well if that is of any use: 如果有任何用处,这也是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>

I would appreciate any help given. 我将不胜感激。

EDIT: 编辑:

LogCat 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)

You have a null pointer exception on 您有一个空指针异常

OutputStream os=socket.getOutputStream();

This is because below line gave an exception due to timeout. 这是因为由于超时,下面的行给出了异常。

socket = new Socket("192.168.1.74", 23);

So socket is still null. 因此套接字仍然为空。 I dont think you should continue operation if socket was not created. 我不认为如果未创建套接字,您应该继续操作。 So change your code like below. 因此,如下更改代码。

 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();
        }

Further do check why you are getting timed out. 进一步检查您为什么超时。 It might be due to device not being on same network, or port not being open 这可能是由于设备不在同一网络上或端口未打开

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM