0

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; } } } 
6
  • post full code EstablishClientSocketforConnection class. Commented May 3, 2014 at 7:28
  • If your class is Activity then where is setContentView() method?? Commented May 3, 2014 at 7:31
  • @ PiYusH GuPtA; yeah I don't need layout. So I don't have setContentView() Commented May 3, 2014 at 7:32
  • 1
    @batuman But If you don't need an UI, you don't need an activity and no need to extends your class as Activity Commented May 3, 2014 at 7:33
  • @PiYusH GuPtA; so how to call this line myPd_ring= new ProgressDialog(mContext); Commented May 3, 2014 at 7:34

3 Answers 3

1

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?

Never instantiate activity classes with new. They won't be useful for anything as they are not properly set up.

If you don't need a UI, you don't need an activity. A progress dialog however is a UI and dialogs need to be hosted in an activity.

If you need an activity, use an Intent to instantiate it.

If you want UI-less progress notification, use an ongoing Notification.

Sign up to request clarification or add additional context in comments.

Comments

0

Use the application context instead of passing the Activity and don't create the activity use new and maybe the use of an activity in this case is completely wrong, you don't use any UI elements. I suggest you to put the progress dialog code in a fragment and inside the fragment the AsyncTask. In this case the fragment should call setreteainInstance(true). In this way you haven't got a leak if the activity is destroyed for example when the orientation change.

Comments

0

You can NOT create a activity with new because all activities are managed by Android OS and have their defined lifecycle.

Also you have to call super.onCreate() in activity's subclass

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.