• 締切済み

androidのオプションメニューについて

androidアプリの作り方を勉強しているのですが、オプションメニューを実装しようと思いonCreateOptionMenuを書いたのですが、エミュレータでメニューボタンを押してもメニューが現れません。onCreateOptionMenu内でTextViewの文字を変更するようにしたのでが、TextViewは変更されず、そもそもonCreateOptionMenuがスルーされているようです。だれか、わかる方がいたらご教授お願いします。 以下ソースです。 シンプルにしたのですが問題がわかりません。 import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; public class Test04Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public boolean onCreateOptionMenu(Menu menu){ super.onCreateOptionsMenu(menu); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } }

  • Java
  • 回答数1
  • ありがとう数0

みんなの回答

  • koko_u_u
  • ベストアンサー率18% (216/1139)
回答No.1

super.onCreateOptionsMenu(menu) を最後によびたまえ

takutaku0928
質問者

補足

onCreateOptionMenu(Menu menu)のreturn true;の前に持ってきたのですが変化ありません。 他に可能性はないでしょうか。

関連するQ&A

  • android inflateメソッドについて

    eclipseから「新規アプリケーション・プロジェクト」でプロジェクトを作成すると下記のコードが自動作成されます。 package com.example.hello; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } この中で、inflateメソッドにカーソルを当てると  void android.view.MenuInflater.inflate(int menuRes, Menu menu) が表示されるので、MenuInflaterクラスのメソッドだと確認できます。 しかし、import文でMenuInflaterクラスを定義していないのになぜ使用できるのでしょうか。

  • androidのアプリを作ってるんですが

    import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView; import android.content.Intent; import android.widget.ImageView; public class GameActivity extends Activity implements OnTouchListener{ private ImageView target; @Override protected void onCreate(Bundle savedInstanceState) { target = (ImageView)findViewById(R.id.imageView1); super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); target.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent motion) { switch(motion.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: break; } return true; } } これからゲーム本体を作っていこうとしてるんですが、onTouchメソッドを実装してから実機で動作しなくなり、先に進めず困っています。なぜこれで動作しないのか私の知識不足でよくわかりません。 回答お願いいたします。

    • ベストアンサー
    • Java
  • アンドロイドのJAVAの基本(文法)について

    アンドロイドの開発を目指して基本から勉強していますが、 本を買っても何のことかさっぱりで困っています。 基本中の基本で恐縮ですが、習熟している方、お助けください。 1.下記はどういう意味でしょうか? public class DBSampleA extends Activity implements View.OnClickListener{ ※implementsの意味を調べても何のことかさっぱりです・・ 2.下記の@Override とは何を意味するのでしょうか? @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); 3.また、onCreate(Bundle savedInstanceState) の中の、 Bundle savedInstanceState はどういう位置付けなのでしょうか?

    • ベストアンサー
    • Java
  • Androidプログラミング アプリが停止する

    eclipseでボタンを押すと画像が表示されるアプリをつくろうとしていましたがボタンをを押すとポップアップで「~を停止します。」と表示されアプリが落ちてしまいます。 Ver4.0を使用しています。 package my.application.imageviewer; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; public class ImageViewer extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void showImage(View view) { ImageView imageView1 = (ImageView)findViewById(R.id.imageView1); imageView1.setImageResource(R.drawable.sample); } } プログラムにミスがあるのでしょうか? 宜しくお願い致します。

  • androidアプリ開発について

    androidアプリ開発初心者です。 やりたいことは、 スタートボタンを押して、自分のお気に入りのページに飛び、(例えばオークションなど)サイトを表示させ、何分間ごとに更新できるループ処理?などができればいいなと考えています。 スタートボタンを押してサイトを表示するまでは何とか作れたのですが、それ以降をどのように作ったらいいのか分かりません。 初心者的な質問で申し訳ないのですが、どのように書いたらよいのか教えてください。 コードはこのようになっています。 <<main.xml>> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="192dp" android:layout_height="wrap_content" android:text="スタート" /> </LinearLayout> <<MainActivity.java>> import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button btn =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Uri uri =Uri.parse("http://"); Intent intent=new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 長文で申し訳ありません。どうぞ宜しくお願い致します。

  • andopoid開発HelloWorld

    andopoidをインストールしてHelloWorldを表示させようとしました。 emulatorは起動できましたが、待っていてもHelloWorldが実行されません。 他のサイトで結構時間がかかると書いてあったので、一時間放置していてもHelloWorldは 実行されず、menuボタンや他のボタンも実行できません。 下記がソースになりますが、何か足りない部分とかありますでしょうか? スイマセン、詳しい方教えて下さい。 package com.android.example; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloWorld extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, World"); setContentView(tv); } }

  • Android開発の最初の段階でつまづく

    アンドロイドのアプリ制作を始めようと思い、なんとか環境を揃え、早速チュートリアルサイトを見ながら始めたのですが、早速、最初のHelloWorldでつまづいてしまいました。 以下ソースコードです。 ********************************************************************************** package helloAndroid.PackageName; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloAndroidActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello,Andloid"); setContentView(tv); } } ********************************************************************************** このように書いたのですが、 The left-hand side of an assignment must be a variable というエラーが下から4行目の tv.setText("Hello,Andloid"); のところで出ます。 調べてはいるのですが、このエラーの意味がいまいちわからず困っています。 アドバイスよろしくお願いします。 私自身はVBを少し触ったことのある程度で、Javaは初めてです。

  • 課題プログラムが強制終了

    現在Androidの学習をしている初心者です。 演習問題の課題プログラムが与えられており、それをこなしています。 ある演習問題でhas application stoppedとなってしまい、強制終了してしまいます。 ググったりしてみたのですが、解決の糸口になるような情報を得られませんでした。 logcatを見た結果、ソースの変更点でNullPointerExceptionが発生していました。 コード的に問題ない(ビルドは通る)ので対策がまったくもって分かりません。 お知恵を拝借したいのですが…。 以下に問題となったソースコードを張り付けておきます。 import android.app.Activity; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.SubMenu; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.EditText; import android.widget.TextView; public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // コンテキストビューアクションの設定 EditText txtMenuTouch = (EditText) findViewById(R.id.sampleText); registerForContextMenu(txtMenuTouch); ←ここでNullPointer発生 // 画面描画 setContentView(R.layout.main); } // -- オプションメニュー関連 @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater iflater = getMenuInflater(); iflater.inflate(R.menu.optionmenu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { final String message; switch (item.getItemId()) { case R.id.opt_menu: message = "オプションメニューが選択されました!"; break; default: message = "その他(入らない)"; break; } TextView txtMenuTouch = (TextView) findViewById(R.id.showMenuTouch); txtMenuTouch.setText(message); return true; } // -- コンテキストメニューとサブメニュー関連 final static int CONTEXT_ITEM_ID1 = 1000; @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("こんてきすとめにゅー"); menu.add(0, CONTEXT_ITEM_ID1, 0, "コンテキストメニュー!"); } @Override public boolean onContextItemSelected(MenuItem item) { final String message; switch (item.getItemId()) { case CONTEXT_ITEM_ID1: message = "こんてきすとめにゅーが指定されました!"; break; default: message = "その他(入らない)"; break; } TextView txtMenuTouch = (TextView) findViewById(R.id.showMenuTouch); txtMenuTouch.setText(message); return true; } // -- その他機能 public void exitButton_click(View view) { this.finish(); } よろしくお願いいたします。

  • アンドロイド JAVA 警告

    アンドロイド 初心者です package com.example.test; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new testView(this)); } class testView extends View { public testView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawLine(0, 0, 100, 50, paint); } } } このプラグラムで"Paint paint = new Paint();"のところで "Avoid object allocations during draw/layout operations (preallocate and reuse instead)" の警告が出ます、これを取る方法はありませんか。

    • ベストアンサー
    • Java
  • [Android]エミュレータで動作しない原因

    こんにちは。java、Android開発初心者です。 「test001」というAndroidプロジェクト、「test001_01」というクラスを作成し、マニフェストを書き換えました。実行すると、再起動して繰り返しても The application has stopped unexpectedly. Please try again. と表示されます。 原因と対処方法を教えていただけませんか。 宜しくお願い致します。 ★test001 Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test001" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.test001.text001_01" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> ★test001_01.java package com.example.test001; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.view.Menu; import android.widget.Button; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; public class test001_01 extends Activity { private Button startButton, stopButton; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sub); startButton = new Button(this); startButton.setText("Button5"); stopButton = new Button(this); stopButton.setText("Button6"); textView = new TextView(this); textView.setText("TextView1"); textView.setBackgroundColor(Color.YELLOW); LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setLayoutParams(linearLayoutParams); linearLayout.addView(startButton, linearLayoutParams); linearLayout.addView(stopButton, linearLayoutParams); linearLayout.addView(textView, linearLayoutParams); setContentView(linearLayout); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }

    • ベストアンサー
    • Java

専門家に質問してみよう