runinuithread找不到textview

时间:2013-09-25 19:25:05

标签: java android timer textview settext

我只是要继续说我的问题可能是愚蠢的。它可能之前也得到了回答,或者很明显它不需要回答,但我似乎无法找到正确的单词组合来找到合适的解决方案。 我没有编程很长时间,所以我可能只是生锈但我真的很难弄清楚这个错误。 我正在写一个Android应用程序,它使用计时器计算你正在赚多少钱。你输入一个小时工资,开始计时,它应该每秒更新一次,显示你已经赚了多少钱。但是,我无法识别计时器任务中的TextView对象,我从编译器中得到一个丢失的符号错误。

这是用户输入工资的主要活动。

    public class IncomeCounter extends Activity
    {
        public final static String EXTRA_MESSAGE = "com.altotech.incomecounter.MESSAGE";
        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void sendMessage(View view){
        Intent intent = new Intent(this, Clock.class);
        EditText editText = (EditText) findViewById(R.id.edittextstring);
        String enteredText = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, enteredText);
        startActivity(intent);

    }
}

这是计时器应该开始计数的第二个活动

public class Clock extends Activity{
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    //get intent from IncomeCounter
    Intent intent = getIntent();
    Bundle bundle = new Bundle();

    //create textview
    TextView textview = new TextView(this);
    textview.setTextSize(40);
    setContentView(textview);


    //initialize currentIncome, put in bundle
    double currentIncome = 0;
    bundle.putDouble("current", currentIncome);

    //create and start timer
    MyTimerTask myTask = new MyTimerTask();
    Timer myTimer = new Timer();
    myTimer.schedule(myTask, 0, 1000);  

    }

    class MyTimerTask extends TimerTask{ 
      public void run() { 
          runOnUiThread(new Runnable(){
              public void run(){

                //get intent and variables
                Intent intent = getIntent(); 
                Bundle bundle = intent.getExtras();  

                //scale incomeRate down to incomePerSecond, get currentIncome and add incomePerSecond to it, putDouble(currentincome), draw it onscreen, then repeat
                String incomeRate = intent.getStringExtra(IncomeCounter.EXTRA_MESSAGE);
                double incomePerSecond = (Double.parseDouble(incomeRate) / 3600);
                double currentIncome = bundle.getDouble("current");
                currentIncome = currentIncome + incomePerSecond;
                bundle.putDouble("current",currentIncome);

                textview.setText(Double.valueOf(currentIncome).toString());

            }
        });

        }
    }

}

textview.setText(...);行是问题所在的位置。只是,每当我查看与runinuithread一起使用的计时器的其他示例时,他们似乎能够setText完全没有问题。也许我在误读,或者我只是愚蠢。但感谢您花时间阅读我的问题和草率的代码。

1 个答案:

答案 0 :(得分:1)

尝试通过在textview或其他任何方法之外声明成员变量onCreate()

public class Clock extends Activity{
@SuppressLint("NewApi")

TextView textview; // declare it here

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

//get intent from IncomeCounter
Intent intent = getIntent();
Bundle bundle = new Bundle();

//create textview
textview = new TextView(this);  // and initialize it here