java.lang.RuntimeException: Unable to start activity ComponentInfo{com.william/c

   最近忙里偷闲学习了一些Android,为以后转做Android打下基础。在今天做一个例子的时候,遇到了一个异常,我看了很久,最后终于找到问题所在。

 

   异常:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.william/com.william.ResultActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x14

 

 解决:TextView 对象中有一个方法为setText(param) ,当你传入一个int类型的值时,代码是不会报错的,但是当你运行着程序的时候,就会报出上面的错误。上网查了一些资料,最后知道为什么会有这样的错误出现,因为这是由于Android 会认为你传入的 int 型的数值是某个 resource 的 id ,那运行时就会出错。

 

public class ResultActivity extends Activity {
	private TextView myResult;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		//得到CalculateActivity传递过来的值
		Intent intent = getIntent();
		String oneValue = intent.getStringExtra("one");
		String twoValue = intent.getStringExtra("two");
		int oneIntValue = Integer.parseInt(oneValue);
		int twoIntValue = Integer.parseInt(twoValue);
		int lastValue = oneIntValue * twoIntValue;
		myResult = (TextView)findViewById(R.id.result);
		myResult.setText(lastValue+"");
	}
	
}

  

    myResult.setText(lastValue+"")代码,如果只myResult.setText(lastValue)写的话,那么这个lastValue值就是一个int类型的值了,那么只需要在后面加上一个空的字符串就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章