CodeforLife
دوشنبه 22 شهریور 1395, 23:17 عصر
با سلام من برنامه ای نوشتم که وقتی روی یه دکمه ای کلیک میشه ؛ در یه thread یه کاری رو انجام بده و کار تمام میشود .
حالا وقتی دوباره روی دکمه کلیک میکنم ، یه exception دارم که میگه threas is start .
یعنی استاپ نشده!!!!
من هرجای برنامه که می تونستم استاپ کردم ولی نشد .
کدهام رو میگذارم .لطف کنید کمکم کنید .
public class CounterThread extends Thread {
public CounterThread() {
}
private int secondCount;
public int getSecondCount() {
return secondCount;
}
public void setSecondCount(int secondCount) {
this.secondCount = secondCount;
}
private TextView txtResult;
public TextView getTxtResult() {
return txtResult;
}
public void setTxtResult(TextView txtResult) {
this.txtResult = txtResult;
}
private Button btnStart;
public Button getBtnStart() {
return btnStart;
}
public void setBtnStart(Button btnStart) {
this.btnStart = btnStart;
}
@Override
public void run() {
super.run();
//
while (secondCount > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
secondCount--;
G.HANDLER.post(new Runnable() {
@Override
public void run() {
int min = (int) Math.floor(secondCount / 60);
int sec = secondCount % 60;
if (sec < 10) {
txtResult.setText("0" + String.valueOf(min) + " : " + "0" + String.valueOf(sec));
} else {
if (min == 0) {
txtResult.setText("0" + String.valueOf(min) + " : " + String.valueOf(sec));
} else if (min != 0 && min < 10) {
txtResult.setText("0" + String.valueOf(min) + " : " + String.valueOf(sec));
} else if (min != 0 && min > 10) {
txtResult.setText(String.valueOf(min) + " : " + String.valueOf(sec));
}
return;
}
if (secondCount == 0) {
btnStart.setEnabled(true);
}
}
});
}
}
EditText edtSecond;
TextView txtResult;
Button btnStart;
CounterThread counterThread = new CounterThread();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//================================================== ====================================
edtSecond = (EditText) findViewById(R.id.edtSecond);
txtResult = (TextView) findViewById(R.id.txtResult);
btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
startWork();
}
}
);
}
private void startWork() {
String secStr = edtSecond.getText().toString();
edtSecond.setText("");
if (secStr.length() > 0 && Integer.parseInt(secStr) <= 1200) {
counterThread.setSecondCount(Integer.parseInt(secS tr));
counterThread.setBtnStart(btnStart);
counterThread.setTxtResult(txtResult);
counterThread.start();
btnStart.setEnabled(false);
return;
} else if (secStr.length() == 0 || Integer.parseInt(secStr) > 1200) {
Toast.makeText(Activity.this, "sdsdsd", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_, menu);
menu.add(0, 1, 1, "راهنما");
return true;
}
@Override
protected void onDestroy() {
super.onDestroy();
counterThread.stop();
}
@Override
protected void onPause() {
super.onPause();
counterThread.stop();
}
حالا وقتی دوباره روی دکمه کلیک میکنم ، یه exception دارم که میگه threas is start .
یعنی استاپ نشده!!!!
من هرجای برنامه که می تونستم استاپ کردم ولی نشد .
کدهام رو میگذارم .لطف کنید کمکم کنید .
public class CounterThread extends Thread {
public CounterThread() {
}
private int secondCount;
public int getSecondCount() {
return secondCount;
}
public void setSecondCount(int secondCount) {
this.secondCount = secondCount;
}
private TextView txtResult;
public TextView getTxtResult() {
return txtResult;
}
public void setTxtResult(TextView txtResult) {
this.txtResult = txtResult;
}
private Button btnStart;
public Button getBtnStart() {
return btnStart;
}
public void setBtnStart(Button btnStart) {
this.btnStart = btnStart;
}
@Override
public void run() {
super.run();
//
while (secondCount > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
secondCount--;
G.HANDLER.post(new Runnable() {
@Override
public void run() {
int min = (int) Math.floor(secondCount / 60);
int sec = secondCount % 60;
if (sec < 10) {
txtResult.setText("0" + String.valueOf(min) + " : " + "0" + String.valueOf(sec));
} else {
if (min == 0) {
txtResult.setText("0" + String.valueOf(min) + " : " + String.valueOf(sec));
} else if (min != 0 && min < 10) {
txtResult.setText("0" + String.valueOf(min) + " : " + String.valueOf(sec));
} else if (min != 0 && min > 10) {
txtResult.setText(String.valueOf(min) + " : " + String.valueOf(sec));
}
return;
}
if (secondCount == 0) {
btnStart.setEnabled(true);
}
}
});
}
}
EditText edtSecond;
TextView txtResult;
Button btnStart;
CounterThread counterThread = new CounterThread();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//================================================== ====================================
edtSecond = (EditText) findViewById(R.id.edtSecond);
txtResult = (TextView) findViewById(R.id.txtResult);
btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
startWork();
}
}
);
}
private void startWork() {
String secStr = edtSecond.getText().toString();
edtSecond.setText("");
if (secStr.length() > 0 && Integer.parseInt(secStr) <= 1200) {
counterThread.setSecondCount(Integer.parseInt(secS tr));
counterThread.setBtnStart(btnStart);
counterThread.setTxtResult(txtResult);
counterThread.start();
btnStart.setEnabled(false);
return;
} else if (secStr.length() == 0 || Integer.parseInt(secStr) > 1200) {
Toast.makeText(Activity.this, "sdsdsd", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_, menu);
menu.add(0, 1, 1, "راهنما");
return true;
}
@Override
protected void onDestroy() {
super.onDestroy();
counterThread.stop();
}
@Override
protected void onPause() {
super.onPause();
counterThread.stop();
}