PDA

View Full Version : مشکل در برنامه زمان سنج thread



CodeforLife
یک شنبه 21 شهریور 1395, 16:21 عصر
سلام
من برنام ای نوشتم که مدت زمان رو از کاربر بگیره و بعد به صورت معکوس بشمره تا برسه به 0
و میخوام در text view نمایش بده برنامه درست کار میکنه ولی زمانیکه میگذارمش توی text view ایراد میگیره !!!
exception دارم که میگه resource رو نمشناسه
براتون کدهام رو میگذارم
لطف کنید کمکم کنید
اول کدهای کلاسم

public class CounterThread extends Thread {

private int secondCounter;

public void setSecondCounter(int secondCounter) {
this.secondCounter = secondCounter;
}

private TextView textView;
public void setTextView(TextView textView){
this.textView=textView;
}
public TextView getTextView(){
return textView;
}



@Override
public void run() {
super.run();
//
while (secondCounter > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
secondCounter--;

if (textView!= null) {
G.HANDLER.post(new Runnable() {
@Override
public void run() {
textView.setText(secondCounter);
Log.i("L",String.valueOf(secondCounter));
}
});
}
}
}





این کدهای activity


CounterThread counterThread = new CounterThread();
EditText edtSecond, edtMinute, edtHour;
TextView txtResult;

@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);
edtMinute = (EditText) findViewById(R.id.edtMinute);
edtHour = (EditText) findViewById(R.id.edtHour);
txtResult = (TextView) findViewById(R.id.txtResult);

Button btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
counterThread.setSecondCounter(Integer.parseInt(ed tSecond.getText().toString()));
counterThread.setTextView(txtResult);
counterThread.start();

}
}
);


}




این هم خطا



android.content.res.Resources$NotFoundException: String resource ID #0x2

at android.content.res.Resources.getText(Resources.ja va:319)

at android.widget.TextView.setText(TextView.java:4421 )

at com.example.parisa.reversecounter.CounterThread$1. run(CounterThread.java:41)

spiderman200700
یک شنبه 21 شهریور 1395, 17:25 عصر
در Thread این خط رو
textView.setText(secondCounter);به این صورت
textView.setText(String.valueOf(secondCounter));
تغییر بدید