package com.example.android_32_chatclinet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity implements OnClickListener {
ListView lv;
EditText et;
MyAdapter adapter;
ArrayList<UserData> list = new ArrayList<UserData>();
BufferedWriter bw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
lv = (ListView) findViewById(R.id.listView1);
et = (EditText) findViewById(R.id.editText1);
adapter = new MyAdapter(this, list);
lv.setAdapter(adapter);
this.connectServer();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.button1){
String text = et.getText().toString();
UserData userData = new UserData("自己说:", text, true);
list.add(userData);
adapter.notifyDataSetChanged();
try {
Log.e("TEST","text" + text);
Log.e("TEST","bw" + bw);
bw.write(text + "\n"); //!!!!!!!!!!!!!!!!!!!!!!!!!这里一直显示NullPointerException!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//连接服务器
private void connectServer(){
new AsyncTask<String, String, Void>(){
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
try {
Socket socket = new Socket(params[0], 3333);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
bw = new BufferedWriter(osw);
// os.write("hello\n".getBytes());
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String text = null;
while((text = br.readLine()) != null){
//Log.e("Test", "text = " + text);
publishProgress(text);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
UserData userData = new UserData("别人说:", values[0], false);
list.add(userData);
adapter.notifyDataSetChanged();
// tv.setText(values[0]);
}
}.execute("192.168.1.104");
}
}
为什么这里的bw为空T T 谁能帮我调调
------解决思路----------------------
bw在点击按钮前没有定义,在之前定义下
------解决思路----------------------
赞同楼上,BufferedWriter bw;只声明了,没初始化。
然后直接用了bw.write(text + "\n");
------解决思路----------------------
你看下有执行到bw那句么,如果socket出问题,可能不会执行。
例如没加访问网络的权限……