java.lang.NullPointerException:尝试调用虚方法' void java.io.Writer.write(java.lang.String中)'在null对象引用上 12-09 07:18:35.259 2419-2419 / com.end.myapplication W / System.err:
在 com.end.myapplication.MainActivity $ SendingRunnable.run(MainActivity.java:124) 12-09 07:18:35.259 2419-2419 / com.end.myapplication W / System.err:
在android.os.Handler.handleCallback(Handler.java:751)12-09 07:18:35.259 2419-2419 / com.end.myapplication W / System.err:at android.os.Handler.dispatchMessage(Handler.java:95)12-09 07:18:35.259 2419-2419 / com.end.myapplication W / System.err:at android.os.Looper.loop(Looper.java:154)12-09 07:18:35.259 2419-2419 / com.end.myapplication W / System.err:at android.app.ActivityThread.main(ActivityThread.java:6119)12-09 07:18:35.259 2419-2419 / com.end.myapplication W / System.err:at java.lang.reflect.Method.invoke(Native Method)12-09 07:18:35.259 2419-2419 / com.end.myapplication W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 12-09 07:18:35.259 2419-2419 / com.end.myapplication W / System.err:
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
MainActivity
package com.end.myapplication;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private String html = "";
private Handler mHandler;
private Socket socket;
private BufferedReader networkReader;
private BufferedWriter networkWriter;
private String ip = "192.168.56.1"; // IP
private int port = 5516; // PORT번호
boolean check=false;
EditText et;
TextView tv;
NetworkThread thread;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.EditText01);
Button btn = (Button) findViewById(R.id.Button01);
tv = (TextView) findViewById(R.id.TextView01);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (et.getText().toString() != null || !et.getText().toString().equals("")) {
check=true;
}
}
});
}
@Override
protected void onResume() {
super.onResume();
new Thread(){
@Override
public void run(){
try {
socket = new Socket(ip, port);
networkWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
networkReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}.start();
thread = new NetworkThread(networkReader, networkWriter);
thread.start();
}
private class NetworkThread extends Thread {
BufferedReader br;
BufferedWriter bw;
Handler mHandler;
SendingRunnable sendingRunnable;
private NetworkThread(BufferedReader br, BufferedWriter bw){
this.br = br;
this.bw = bw;
mHandler = new Handler();
sendingRunnable = new SendingRunnable(bw);
}
@Override
public void run() {
super.run();
boolean isError = false;
String result = "";
String error = "";
try{
this.bw.write("CONNECTED\n");
this.bw.flush();
}catch (Exception e){
e.printStackTrace();
}
while (true){
try{
if (check){
mHandler.post(sendingRunnable);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
private class SendingRunnable implements Runnable {
BufferedWriter bw;
private SendingRunnable(BufferedWriter bw){
this.bw = bw;
}
@Override
public void run() {
String formula = et.getText().toString();
try{
bw.write(formula+"\n");
bw.flush();
et.setText("");
}catch (Exception e){e.printStackTrace();}
}
}
}
答案 0 :(得分:1)
您正在同时启动两个线程,其中第一个线程正在初始化第二个线程中的资源。
解决方案:从第一个线程
开始第二个线程或
在NetworkThread
线程中移动套接字连接和读取器,编写器代码(不需要两个线程)
答案 1 :(得分:0)
您的线程使用中可能存在竞争条件。尝试删除以前的AzureWebJobsStorage
。
更改
Thread
到
new Thread(){
@Override
public void run(){
try {
socket = new Socket(ip, port);
networkWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
networkReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e1) {
e1.printStackTrace();
}
}
}.start();