最近androidアプリをちょっと勉強中です。
そこで本日少しはまったところをメモ
現在以下のページを参考に勉強していますが
連載:世界を目指せ!Androidアプリ開発入門
第6回の「タイトルバーにアイコンをつける」のところをサンプルプログラムどおりに書いても実行時例外が発生してしまいきちんと動きません。
ERROR/AndroidRuntime(402): FATAL EXCEPTION: main ERROR/AndroidRuntime(402): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.skeletonapp/com.example.android.skeletonapp.SkeletonActivity}: java.lang.RuntimeException: The feature has not been requested ERROR/AndroidRuntime(402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) ERROR/AndroidRuntime(402): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) ERROR/AndroidRuntime(402): at android.app.ActivityThread.access$2300(ActivityThread.java:125) ERROR/AndroidRuntime(402): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) ERROR/AndroidRuntime(402): at android.os.Handler.dispatchMessage(Handler.java:99) ERROR/AndroidRuntime(402): at android.os.Looper.loop(Looper.java:123) ERROR/AndroidRuntime(402): at android.app.ActivityThread.main(ActivityThread.java:4627) ERROR/AndroidRuntime(402): at java.lang.reflect.Method.invokeNative(Native Method) ERROR/AndroidRuntime(402): at java.lang.reflect.Method.invoke(Method.java:521) ERROR/AndroidRuntime(402): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) ERROR/AndroidRuntime(402): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) ERROR/AndroidRuntime(402): at dalvik.system.NativeStart.main(Native Method) ERROR/AndroidRuntime(402): Caused by: java.lang.RuntimeException: The feature has not been requested ERROR/AndroidRuntime(402): at com.android.internal.policy.impl.PhoneWindow.getDrawableState(PhoneWindow.java:2256) ERROR/AndroidRuntime(402): at com.android.internal.policy.impl.PhoneWindow.setFeatureDrawableResource(PhoneWindow.java:874) ERROR/AndroidRuntime(402): at com.example.android.skeletonapp.SkeletonActivity.onCreate(SkeletonActivity.java:56) ERROR/AndroidRuntime(402): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) ERROR/AndroidRuntime(402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) ... 11 more
いろいろ他のページも調べたところ参考ページのコードはWindow.requestFeatureメソッドでウインドウの項目を取得(?)していますが、他のページを見るとActivity.requestWindowFeatureで取得しているコードもあり、そちらに変えるとうまくアイコンを貼り付けることができました。
/** Called with the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // アイコンの画像をアプリタイトルにつける requestWindowFeature(Window.FEATURE_LEFT_ICON); // こちらは正常に動く //getWindow().requestFeature(Window.FEATURE_RIGHT_ICON); // こちらだと例外発生 // Inflate our UI from its XML layout description. setContentView(R.layout.skeleton_activity); getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon); // Find the text editor view inside the layout, because we // want to do various programmatic things with it. mEditor = (EditText) findViewById(R.id.editor); // タイトル表示する setTitle(R.string.title_main); //setContentView(R.layout.skeleton_activity); // Hook up button presses to the appropriate event handler. ((Button) findViewById(R.id.back)).setOnClickListener(mBackListener); ((Button) findViewById(R.id.clear)).setOnClickListener(mClearListener); mEditor.setText(getText(R.string.main_label)); }</script>
実行環境が微妙に違うので、androidのversionの違いなのかなと漠然と考えていますが。。。
うーん、むずかしい。。。