質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

661閲覧

Toastが表示されない

j06110611

総合スコア54

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2020/03/09 07:45

やりたいこと

activity_mainに表示されるゴミ箱を押すと、Toastが表示されるようにしたい。

現状

ゴミ箱を押しても、何もlogcatにエラーが出ていません。

MainActivity

1public class MainActivity extends AppCompatActivity { 2 3 private ArrayList data = new ArrayList<>(); 4 private static final int REQUEST_CODE = 1; 5 private ArrayAdapter adapter; 6 7 @Override 8 protected void onActivityResult(int request_Code, int resultCode, Intent result){ 9 switch(request_Code){ 10 //MakeQuizActivityから戻ってきた場合 11 case(REQUEST_CODE): 12 //クイズを作るボタンを押して戻ってきたときの処理 13 if(resultCode == RESULT_OK){ 14 //setContentView(R.layout.activity_main); 15 //String test = result.getStringExtra("INPUT_TITLE"); 16 //System.out.println(test); 17 data.add(result.getStringExtra("INPUT_TITLE")); 18 adapter.notifyDataSetChanged(); 19 } 20 //押されなかったときの処理 21 else if(resultCode == RESULT_CANCELED){ 22 //setContentView(R.layout.activity_main); 23 } 24 break; 25 } 26 } 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 31 // リスト項目とListViewを対応付けるArrayAdapterを用意する 32 adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);; 33 // ListViewにArrayAdapterを設定する 34 ListView listView = (ListView) findViewById(R.id.listView); 35 listView.setAdapter(adapter); 36 Button button = (Button) findViewById(R.id.makeQuiz_Button1); 37 button.setOnClickListener(new View.OnClickListener() { 38 @Override 39 public void onClick(View v) { 40 Intent intent = new Intent(MainActivity.this, com.test.quizmaker.MakeQuizActivity.class); 41 startActivityForResult(intent, REQUEST_CODE); 42 } 43 }); 44 } 45 //オプションメニューを作成 46 public boolean onCreateOptionsMenu(Menu menu){ 47 //menuにcustom_menuレイアウトを適用 48 getMenuInflater().inflate(R.menu.custom_menu, menu); 49 //オプションメニューを表示する場合はtrue 50 return true; 51 } 52 //メニュー選択時の処理 トースト表示 53 public boolean onOptionsSelected(MenuItem menuItem){ 54 //押されたメニューのIDで処理を振り分ける 55 if(menuItem.getItemId() == R.id.action_delete){ 56 Toast toast = Toast.makeText(MainActivity.this,"ゴミ箱を選択",Toast.LENGTH_SHORT); 57 toast.show(); 58 } 59 return true; 60 } 61} 62

activity_main

1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <Button 10 android:id="@+id/quizStart_Button" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_marginStart="8dp" 14 android:layout_marginTop="12dp" 15 android:layout_marginEnd="8dp" 16 android:layout_marginBottom="8dp" 17 android:text="@string/quizStart_Button" 18 app:layout_constraintBottom_toBottomOf="parent" 19 app:layout_constraintEnd_toEndOf="parent" 20 app:layout_constraintStart_toEndOf="@+id/update_Button" 21 app:layout_constraintTop_toBottomOf="@+id/listView" /> 22 23 <Button 24 android:id="@+id/makeQuiz_Button1" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_marginStart="8dp" 28 android:layout_marginTop="12dp" 29 android:layout_marginEnd="8dp" 30 android:layout_marginBottom="8dp" 31 android:text="@string/makeQuiz1_Button" 32 app:layout_constraintBottom_toBottomOf="parent" 33 app:layout_constraintEnd_toStartOf="@+id/update_Button" 34 app:layout_constraintStart_toStartOf="parent" 35 app:layout_constraintTop_toBottomOf="@+id/listView" /> 36 37 <Button 38 android:id="@+id/update_Button" 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 android:layout_marginStart="8dp" 42 android:layout_marginTop="8dp" 43 android:layout_marginEnd="8dp" 44 android:layout_marginBottom="8dp" 45 android:text="@string/update_Button" 46 app:layout_constraintBottom_toBottomOf="parent" 47 app:layout_constraintEnd_toEndOf="parent" 48 app:layout_constraintStart_toStartOf="parent" 49 app:layout_constraintTop_toBottomOf="@+id/listView" 50 app:layout_constraintVertical_bias="0.75" /> 51 52 <ListView 53 android:id="@+id/listView" 54 android:layout_width="match_parent" 55 android:layout_height="431dp" 56 android:layout_marginStart="8dp" 57 android:layout_marginTop="8dp" 58 android:layout_marginEnd="8dp" 59 android:divider="@color/design_default_color_primary_dark" 60 app:layout_constraintEnd_toEndOf="parent" 61 app:layout_constraintStart_toStartOf="parent" 62 app:layout_constraintTop_toTopOf="parent" /> 63 64 <TextView 65 android:id="@+id/text_write" 66 android:layout_width="wrap_content" 67 android:layout_height="wrap_content" /> 68 69</android.support.constraint.ConstraintLayout>

custom_menu

1<?xml version="1.0" encoding="utf-8"?> 2<menu xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto"> 4 xmlns:app="http://schemas.android.com/apk/res-auto"> 5 <item 6 android:id="@+id/action_delete" 7 android:orderInCategory="1" 8 android:icon="@android:drawable/ic_menu_delete" 9 android:title="DELETE" 10 app:showAsAction="always" /> 11</menu>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

メソッド名が間違ってます。

Java

1 //メニュー選択時の処理 トースト表示 2 public boolean onOptionsSelected(MenuItem menuItem){

正しくは onOptionsItemSelected です。(Item が抜けてます。)

Java

1 @Override 2 public boolean onOptionsItemSelected(MenuItem menuItem) {

メソッドの前に @Override を付けると、オーバーライドするつもりがスペルミスでオーバーライドできてなかった時にエラーになって教えてくれます。また、オーバーライドするメソッド名は全部打ち込まずに Code メニューの Generate... やコード補完を利用すると良いでしょう。
参考: 第16回 エディタの話[その8]─いろいろなコード生成方法と比較:Android Studio最速入門~効率的にコーディングするための使い方|gihyo.jp … 技術評論社

投稿2020/03/09 08:56

hoshi-takanori

総合スコア7893

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

j06110611

2020/03/09 10:26

助かりました!ありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問