setText不向TextView显示值

时间:2012-06-05 00:29:42

标签: java android eclipse

让我先言一下,我是Android的编程新手。我一直在尽职尽责地研究无济于事。我有下面的源代码,我遇到了返回返回值的问题。

我在eclipse上编写了代码,并没有触发任何错误。但是当我构建下面的代码时,它会返回错误。检查调试视图中的值后,我可以看到没有绑定到TextView的正确值。

public class MyFirstActivity extends Activity {
/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      calculateResult(2012, 9, 29);
  }
  private void calculateResult(int year, int month, int day) {
      long days = 0L;
      int returning = 0;

      java.util.Calendar cal = new java.util.GregorianCalendar(year, month-1, day);

      long todayMI = new java.util.Date().getTime();
      long calMI = cal.getTimeInMillis();
      long millDiff = calMI - todayMI;

      if (millDiff < 0) {
            returning = 0;
      } else {
            days = millDiff / 1000 / 60 / 60;
            returning = (int) Math.ceil(days / 24f);
      }

      TextView days_int_remaining = (TextView) findViewById(R.id.days_int_remaining);
      days_int_remaining.setText(returning);
  }
}

如果有帮助,这是布局XML上的TextView:

<TextView
      android:id="@+id/days_int_remaining"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:layout_gravity="center_horizontal|center_vertical"
      android:shadowColor="#5000"
      android:shadowDx="4"
      android:shadowDy="3"
      android:shadowRadius="1"
      android:textColor="#fff"
      android:textSize="70dip"
      android:textStyle="bold"
/>

我可能很遗憾。任何帮助我进一步理解代码的事情都值得赞赏!

4 个答案:

答案 0 :(得分:2)

我相信你得到的错误是“运行时错误 - 关闭应用程序”。

如果你看一下重载的方法setText(...),你会发现它需要带CharSequence的参数(这可能是你想要的)和int resId(这是你提供的)

resId - 将在/values/strings.xml中找到具有给定name属性的资源。

决议是提供String

days_int_remaining.setText(String.valueOf(returning));

答案 1 :(得分:1)

returning int转换为字符串。

答案 2 :(得分:1)

只需使用

TextView days_int_remaining = (TextView) findViewById(R.id.days_int_remaining);
days_int_remaining.setText(String.valueOf(returning));

而不是

TextView days_int_remaining = (TextView) findViewById(R.id.days_int_remaining);
days_int_remaining.setText(returning);

答案 3 :(得分:0)

试试这段代码:

基本上,在调用setText();

之前,必须将int值转换为字符串
public class MyFirstActivity extends Activity {
/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      TextView days_int_remaining = (TextView) findViewById(R.id.days_int_remaining);
      int resultat = calculateResult(2012, 9, 29);
      days_int_remaining.setText(Integer.toString(resultat));
  }
  private int calculateResult(int year, int month, int day) {
      long days = 0L;
      int returning = 0;

      java.util.Calendar cal = new java.util.GregorianCalendar(year, month-1, day);

      long todayMI = new java.util.Date().getTime();
      long calMI = cal.getTimeInMillis();
      long millDiff = calMI - todayMI;

      if (millDiff < 0) {
            returning = 0;
      } else {
            days = millDiff / 1000 / 60 / 60;
            returning = (int) Math.ceil(days / 24f);
      }

       return returning;

  }
}