[Java for Android App Development] warlock9_0:
public class MainActivity extends Activity implements SensorEventListener{

private static final int SERVERPORT = 8222;
private static final String SERVER_IP = "192.168.0.101";

private SensorManager mSensorManager;
private Sensor mAccelerometer;
private TextView tv ;

public ServerSocket serverSocket=null;
public Socket socket = null;

public BufferedReader in ;
public BufferedWriter out;
public PrintWriter pw ;

public FileWriter writer=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new MyTask().execute();
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

public void onStopClick(View view) {
mSensorManager.unregisterListener(this);
}
protected void onResume() {
super.onResume();

}

protected void onStop(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mSensorManager.unregisterListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private class MyTask extends AsyncTask<Void, Void,AllInOne>

{

@Override
protected AllInOne doInBackground(Void... arg0) {
// TODO Auto-generated method stub

try {
serverSocket = new ServerSocket(8222);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
socket=serverSocket.accept();
Log.i("TcpServer", "CONNECTED");

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(),true);

//out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

if(!in.ready())
{
Log.i("TcpServer", "READER IS NOT READY");
}

final String g;
final String c;

String b = null;

Log.i("TcpServer", "GOING");

g=in.readLine();

//Log.i("TcpServer", in.readLine());

runOnUiThread(new Runnable() {
public void run() {

Toast.makeText(getApplicationContext(),g, Toast.LENGTH_LONG).show();

}
});

Log.i("TcpServer", "RECEIVED");

runOnUiThread(new Runnable() {
public void run() {

Toast.makeText(getApplicationContext(),g, Toast.LENGTH_LONG).show();

}
});

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

String response = e.getCause().toString();

Log.i("TCP",response);
}

return new AllInOne(socket,in,pw);
}

protected void onPostExecute(AllInOne result) {
socket = result.so;
in = result.re;
pw = result.pr;

}

}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {

if(pw!=null) pw.println("egaga");
else Log.e("PrintWriter", null);
}

public void CloseConn() throws IOException //not used
{
pw.flush();
in.close();
socket.close();
serverSocket.close();

}

private class AllInOne {

public Socket so;
public BufferedReader re;
public PrintWriter pr;

public AllInOne(Socket s, BufferedReader r, PrintWriter p ){
this.so = s;
this.re = r;
this.pr = p;
}

}

}

try this

since you are initializing in and pw inside the doinbackground, you have to pass them to the main thread

so i added another class to hold all these and i pass them on result

finally you also have to check in the onsensorchanged if the pw is null because the asynctask may have not finished yet when this is called