我只是继续说我的问题可能很愚蠢。它可能之前也已经被回答过,或者很明显不需要回答,但是我似乎无法找到合适的单词组合来找到合适的解决方案。
我已经很长时间没有编程了,所以我可能会生疏,但是要弄清楚这个错误确实很困难。
我正在编写一个使用计时器来计算您赚多少钱的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的功能。也许我读错了,或者我只是傻了。但是,感谢您抽出宝贵的时间阅读我的问题和草率的代码。

最佳答案

尝试通过在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

10-02 04:26