如何设置文本一个整数并得到int而不会出错

我试图从整数中获取意图。 字符串获取意图工作正常并显示良好,但当我把整数我得到一个强制关闭错误。

package kfc.project; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.EditText; import android.widget.TextView; public class productdetail extends Activity{ @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.productdetail); //stuff to get intent Intent receivedIntent = getIntent(); String productName = receivedIntent.getStringExtra("name"); int productCalories = receivedIntent.getIntExtra("calories",0); Bundle extras = getIntent().getExtras(); String name = extras.getString("name"); if (name != null) { TextView text1 = (TextView) findViewById(R.id.servingsize); text1.setText(productName); } //int calories = extras.getInt("calories"); TextView text1 = (TextView) findViewById(R.id.calories); text1.setText(productCalories); } } 

 TextView text1 = (TextView) findViewById(R.id.calories); text1.setText(""+productCalories); 

要么

 text1.setText(String.valueOf(productCalories)); 

方法setText(int)查找具有该特定int-id的字符串资源。 你想要做的是调用setText(String)方法,该方法取代提供的字符串。

您可以通过多种方式将int转换为String,但我更喜欢这个:

  TextView text1 = (TextView) findViewById(R.id.calories); text1.setText(String.valueOf(productCalories)); 

编辑:似乎有人已经回答。