2013-09-25 1 views
0

저는 앞으로 가서 내 질문이 아마도 바보라고 말할 것입니다. 이전에 답을 얻었거나 대답 할 필요가 없으므로 분명한 사실이지만 적절한 해결책을 찾기 위해 올바른 단어 조합을 찾지 못하는 것 같습니다. 오랫동안 프로그래밍하지 못했기 때문에 녹슬지는 모르지만이 실수를 알아 내는데 어려움을 겪고 있습니다. 나는 얼마나 많은 돈을 벌고 있는지를 계산하는 타이머를 사용하는 안드로이드 앱을 쓰고있다. 당신은 시간당 임금을 입력하고, 시계를 시작하고, 당신이 얼마나 많은 돈을 벌었는지 보여주는 매 초마다 업데이트해야합니다. 그러나 타이머 작업의 TextView 개체를 인식 할 수 없으며 컴파일러에서 누락 된 기호 오류가 발생합니다.runinuithread가 텍스트 뷰를 찾을 수 없습니다

사용자가 임금을 입력하는 주요 활동은 다음과 같습니다.

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); 

    } 
} 

여기에 타이머가 textview.setText(...); 라인은

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()); 

      } 
     }); 

     } 
    } 

} 

세기 시작하도록되어 두 번째 활동을 어디에서 문제의가. 단지 runinuithread과 함께 사용되는 타이머의 다른 예제를 살펴볼 때마다 그들은 전혀 문제없이 setText 수 있습니다. 어쩌면 내가 잘못 읽은 것일 수도 있고 아니면 그냥 바보 일 수도 있습니다. 하지만 내 질문과 엉성한 코드를 읽을 시간을내어 주셔서 감사합니다.

답변

1

textviewonCreate() 외부에서 선언하거나 다른 방법으로 멤버 변수로 변경해보십시오.

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 
+0

감사합니다. 넌 최고야. 이제 내가 실행하고있어 실제로 textview isnt 실제로 매초마다 업데이 트하거나 currentIncome 실제로 증가하지 볼 수 있습니다. 그러나, 나는 그것을 여기에서 혼자서 얻을 수 있다고 생각합니다. 다시 한번 감사드립니다. – user2816570

+0

당신을 진심으로 환영합니다. 행운을 빕니다! 그리고 자신감에 조금 노력;) – codeMagic

관련 문제