2015-03-13 62 views
1

我特地在网上获取一个值,但我还没有找到摆脱TextView的从一个TextView

错误是该值的方式:无法解析方法的valueOf()

time_left = timeLeftNumber.valueOf() - 1; 

package com.example.sensational.timetopress; 

import android.os.CountDownTimer; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.TextView; 


public class press_screen extends ActionBarActivity { 

    private int time_left; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_press_screen); 

    } 

    //Create Timer 


    CountDownTimer timer = new CountDownTimer(30000, 1000) { 
     @Override 
     public void onTick(long millisUntilFinished) { 
      TextView amountOfTaps = (TextView) findViewById(R.id.amount_of_taps); 
      TextView timeLeftNumber = (TextView) findViewById(R.id.time_left_number_tv); 
      time_left = timeLeftNumber.valueOf() - 1; 
      timeLeftNumber.setText(); 
     } 

     @Override 
     public void onFinish() { 

     } 
    }; 
+0

什么是错误? – Lal 2015-03-13 18:03:54

+0

@Lal我只是把它放在 – Celeriac 2015-03-13 18:06:16

回答

0

TextView没有valueOf()方法。您可以进行以下操作:

TextView amountOfTaps = (TextView) findViewById(R.id.amount_of_taps); 
    TextView timeLeftNumber = (TextView) findViewById(R.id.time_left_number_tv); 
    time_left = Integer.valueOf(timeLeftNumber.getText().toString()) - 1; 
    timeLeftNumber.setText(); 
0

只要使用方法getText()它返回一个CharSequence

time_left = timeLeftNumber.getText().toString(); 

为了得到一个Integer值使用

time_left = parseInt(timeLeftNumber.getText().toString(), 10) -1; 

因为它可能会抛出一个NumberFormatException(最终版):

try { 
    time_left = parseInt(timeLeftNumber.getText().toString(), 10) -1; 

} catch (NumberFormatException e) { 
    // handle invalid numbers; set reasonable default value 
    time_left = 0; 
} 
+0

以上我这样做,但我想 - 1的结果。我做time_left = timeLeftNumber.getText()。toString() - 1;但它输出运算符' - '不能应用于java.lang.String @Trinimon – Celeriac 2015-03-13 18:08:37

+0

@SexyBeast:对不起,我没有完全准备好:第三个版本是你需要的:) – Trinimon 2015-03-13 18:11:42

0

你需要做的:

TIME_LEFT =的Integer.parseInt(timeLeftNumber.getText()的toString()) - 1;

0

TextView获得价值,你应该使用getText()方法。它返回CharSequence,可能很容易地改变,以String,然后你必须转换得到的值来诠释这样

time_left = Integer.parseInt(timeLeftNumber.getText().toString()

这种方法可能会抛出NumberFormatException的,如果输入的文本是不是一个数字,所以你应该里面把这个调用try-catch块。