问:错误:没有与ID:10相关联的资源。注意:setText(int)与setText(String)不同。
MainActivity.java
导入 android.content.Intent ;
导入 android.view.View ;
公共类MainActivity扩展了Activity { private static final String KEY_USERENTRY = “ KEY_USERENTRY” ; 公共TextView mTextView ; @Override受保护的void onCreate (Bundle savedInstanceState ){超级。onCreate (savedInstanceState ); 的setContentView (ř 。布局
。activity_main );
mTextView = (TextView的)findViewById (ř 。ID 。TextView的); } @Override protected void onSaveInstanceState (捆绑outState ){超级。onSaveInstanceState (outState ); outState 。putInt (KEY_USERENTRY ,10 ); } @覆盖保护无效onRestoreInstanceState (包
savedInstanceState ) {
超级。onRestoreInstanceState (savedInstanceState ); mTextView 。的setText (savedInstanceState 。getInt (KEY_USERENTRY )); } }
答:mTextView 。的setText (savedInstanceState 。getInt (KEY_USERENTRY ) + “” )
我们必须将+“”放在getInt()方法之外。getInt()方法将只接受一个键值,然后我们将其隐式转换为字符串