I have a class extended from Activity as
public class EstablishClientSocketforConnection extends Activity{ ClientThread task = new ClientThread(EstablishClientSocketforConnection.this); task.execute(); } I have an AsyncTack and I like to run progress dialog inside the AsyncTask
public class ClientThread extends AsyncTask<Integer, Integer, Integer> { private Context mContext; ProgressDialog myPd_ring; public ClientThread(Context context) { mContext = context; } protected void onPreExecute() { myPd_ring= new ProgressDialog(mContext); myPd_ring.setCancelable(true); myPd_ring.setTitle("Please wait!"); myPd_ring.setMessage("Connecting..."); myPd_ring.setIndeterminate(true); myPd_ring.setProgressStyle(ProgressDialog.STYLE_SPINNER); myPd_ring.show(); } } The problem is with mContext and when the line myPd_ring= new ProgressDialog(mContext); is run, the program crash. I don't create the EstablishClientSocketforConnection from Intent and just create with new as I don't need xml layout. What could be wrong? Is the problem because of not starting the EstablishClientSocketforConnection from Intent?
EDIT: The full code is
public class EstablishClientSocketforConnection extends Activity{ private boolean connected = false; private String serverIp = ""; private int PORT = -1; public Socket socket; public String st; public void onCreate(String serverIpAddress, int REDIRECTED_SERVERPORT) { serverIp = serverIpAddress; PORT = REDIRECTED_SERVERPORT; if (!serverIp.equals("")) { ClientThread task = new ClientThread(EstablishClientSocketforConnection.this); task.execute(); } } public void disConnect(){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public class ClientThread extends AsyncTask<Integer, Integer, Integer> { private Context mContext; ProgressDialog myPd_ring; public ClientThread(Context context) { mContext = context; } protected void onProgressUpdate(Integer... progress) { } protected void onPreExecute() { myPd_ring= new ProgressDialog(mContext); myPd_ring.setCancelable(true); myPd_ring.setTitle("Please wait!"); myPd_ring.setMessage("Connecting..."); myPd_ring.setIndeterminate(true); myPd_ring.setProgressStyle(ProgressDialog.STYLE_SPINNER); myPd_ring.show(); } protected void onPostExecute(Integer result) { if (myPd_ring.isShowing()) { myPd_ring.dismiss(); } } @Override protected Integer doInBackground(Integer... params) { st = null; try { InetAddress serverAddr = InetAddress.getByName(serverIp); Log.d("ClientActivity", "C: Connecting..."); socket = new Socket(serverAddr, PORT); connected = true; while (connected) { try { Log.d("ClientActivity", "C: Sending command."); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket .getOutputStream())), true); // WHERE YOU ISSUE THE COMMANDS out.println("Hey Server!"); Log.d("ClientActivity", "C: Sent."); do{ BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); st = input.readLine(); }while(st == null); Log.d("Server response received", "C: Received."); } catch (Exception e) { Log.e("ClientActivity", "S: Error", e); } } Log.d("ClientActivity", "C: Closed."); } catch (Exception e) { Log.e("ClientActivity", "C: Error", e); st = e.toString(); connected = false; } return null; } } }
EstablishClientSocketforConnectionclass.Activitythen where issetContentView()method??UI, you don't need an activity and no need to extends your class asActivity