开发者社区> 问答> 正文

一个关于Android编成问题

代码如下:

public class get extends MainActivity {
Socket socket;
BufferedReader in;
private EditText OP;
EditText dose=(EditText)findViewById(R.id.editText);
public get() {
try {
Socket socket = new Socket("192.168.11.254", 8080);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
new gd().start();
} catch (IOException e) {
e.printStackTrace();
}
}
class gd extends Thread {
    public void run() {
        while (this.isAlive()) {
            try {
                String kk = in.readLine();
                int k = (kk.lastIndexOf("dose"));
                String z;
                z = kk.substring(k + 5, k + 10);
                Thread.sleep(50);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
}

提示如下:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

如何解决?

展开
收起
蛮大人123 2016-06-08 14:07:59 2597 0
2 条回答
写回答
取消 提交回答
  • 什么什么问题
    2019-07-17 19:31:50
    赞同 展开评论 打赏
  • 我说我不帅他们就打我,还说我虚伪

    Socket socketBufferedReader in实例化了两次,

    Socket socket = new Socket("192.168.11.254", 8080);`
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    把这两句改为

    socket = new Socket("192.168.11.254", 8080);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    2019-07-17 19:31:50
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Android组件化实现 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载