1

Let me preface that I'm new to programming for Android. I've been doing my due diligence to research to no avail. I have the source code below and I'm having issues with returning the value for returning.

I have the code laid out on eclipse and it's not triggering any errors. But when I build the code below, it comes back with an error. After inspecting the values in debug view, I can see the proper values just not binded to the 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);
  }
}

If it helps, here's the TextView on my layout XML:

<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"
/>

It may be something simple that I'm missing. Anything to help me further understand the code is appreciated!

4

4 回答 4

2

I believe that the error you get is "runtime-error - close application".

If you look at method setText(...) that is overloaded you will see that it takes arguments with CharSequence (this is probably what you want) and int resId (this is what you provide).

resId - will seek if there is an resource in /values/strings.xml with given name attribute.

The resolution is to provide String:

days_int_remaining.setText(String.valueOf(returning));
于 2012-06-05T00:43:35.087 回答
1

Turn your returning int into a string.

于 2012-06-05T00:42:25.757 回答
1

just use

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

instead of

TextView days_int_remaining = (TextView) findViewById(R.id.days_int_remaining);
days_int_remaining.setText(returning);
于 2012-06-05T00:45:39.703 回答
0

Try this code :

Basically you have to convert your int value to a string before calling setText();

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;

  }
}
于 2012-06-05T00:47:24.290 回答