[Java for Android App Development] flavglen:
still null

here is my entire code..i have done exactly what u said...still socket null in OnsensorChanged()

package com.example.sensorsmart;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

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,Socket>

{

@Override
protected Socket 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 socket;
}

protected void onPostExecute(Socket result) {
socket = result;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream(),true);

}

}

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

}

@Override
public void onSensorChanged(SensorEvent event) {

pw.println("egaga");

}

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

}

}