0

我只是继续说我的问题可能很愚蠢。它可能以前也被回答过,或者很明显不需要回答,但我似乎无法找到正确的单词组合来找到合适的解决方案。我很久没有编程了,所以我可能只是生疏了,但我真的很难弄清楚这个错误。我正在编写一个 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使用。也许我看错了,或者我只是愚蠢。但是感谢您花时间阅读我的问题和草率的代码。

4

1 回答 1

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
于 2013-09-25T19:28:18.087 回答