我的课程从Activity
扩展为
public class EstablishClientSocketforConnection extends Activity{
ClientThread task = new ClientThread(EstablishClientSocketforConnection.this);
task.execute();
}
我有一个AsyncTack
,我想在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();
}
}
问题在于mContext以及行myPd_ring= new ProgressDialog(mContext);
运行,程序崩溃。我不会从EstablishClientSocketforConnection
创建Intent
,只使用new
创建,因为我不需要xml布局。可能有什么不对?问题是因为没有从EstablishClientSocketforConnection
开始Intent
?
编辑: 完整的代码是
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;
}
}
}
答案 0 :(得分:1)
问题在于mContext和行myPd_ring = new ProgressDialog(mContext);运行,程序崩溃。我没有从Intent创建EstablishClientSocketforConnection,只是用new创建,因为我不需要xml布局。可能有什么不对?问题是因为没有从Intent启动EstablishClientSocketforConnection吗?
永远不要使用new
实例化活动类。它们对任何东西都没用,因为它们没有正确设置。
如果您不需要UI,则不需要活动。然而,进度对话框是一个UI,对话框需要在活动中托管。
如果您需要某项活动,请使用Intent
对其进行实例化。
如果您想要无UI进度通知,请使用正在进行的Notification
。
答案 1 :(得分:0)
使用应用程序上下文而不是传递Activity并且不创建活动使用new并且在这种情况下使用活动可能完全错误,您不使用任何UI元素。我建议你将进度对话框代码放在一个片段中,在片段里面放入AsyncTask。在这种情况下,片段应该调用setreteainInstance(true)。这样,如果活动被破坏,例如当方向改变时,你就没有泄漏。
答案 2 :(得分:0)
您无法使用new创建活动,因为所有活动都是由Android操作系统管理的,并且具有已定义的生命周期。
此外,您还必须在activity的子类中调用super.onCreate()