0

レシピ本を書いていますが、問題が発生しました。メールアドレスと件名が事前に入力されたメールを送信するはずのメニュー項目をクリックしても、何も起こりません...

理由は何ですか???

public class recipedisplayscreen extends Activity {

TextView EmailAddress;

TextView EmailSubject;

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recipedisplayscreen);

        TextView MethodDisplay = (TextView) findViewById(R.id.textView3);
        TextView IngredientsDisplay = (TextView) findViewById(R.id.textView5);


        Intent i = getIntent();
        String Ingredients = i.getStringExtra("textView1");
        String Method = i.getStringExtra("textView2");
        Log.e("recipedisplayscreen", Ingredients + "." + Method);

        MethodDisplay.setText(Method);
        IngredientsDisplay.setText(Ingredients);

        EmailAddress=(TextView) findViewById(R.id.textView2); 
        EmailSubject=(TextView) findViewById(R.id.textView4); 


        ActionBar actionBar = getActionBar();
        setTitle(R.string.title);
        actionBar.setDisplayHomeAsUpEnabled(true);
        setTitle(Method);}

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    // App icon in action bar clicked; go home
                    Intent intent = new Intent(this, MainScreen.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }

        public boolean onOptionsItemSelected1(MenuItem recipe_suggest) {
            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
            emailIntent.setType("plain/text"); 
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ EmailAddress.getText().toString()}); 
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EmailSubject.getText()); 

            startActivity(Intent.createChooser(emailIntent, "Send mail..."));
            return true;
        }




     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.recipe_menu1, menu);
            return true;         
    }    
}
4

4 に答える 4

4

これはあなたが望むものだと思います:D onOptionsItemSelected1 を削除し、 onOptionsItemSelectedをこれに置き換えます。これは、recipe_suggest がメニュー項目の ID であると想定していますか?

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // App icon in action bar clicked; go home
                Intent intent = new Intent(this, MainScreen.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            case R.id.recipe_suggest:
                //Other menu item I believe
                final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
                emailIntent.setType("plain/text"); 
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{                 EmailAddress.getText().toString()}); 
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EmailSubject.getText()); 

                startActivity(Intent.createChooser(emailIntent, "Send mail..."));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

switch が行うことは、「switch」内のオブジェクトをケースに一致させることであり、「switch」に一致するケースが実行されるため、メニュー項目 ID (MenuItem item; item.getItemId()) が ID または android に一致する場合.R.id.homeまたはR.id.recipe_suggest(Android Rファイルを参照しているので、アクションバーを使用していると思いますか?)これは、私の知る限りではどのように行うべきかです:)

于 2012-05-04T11:58:40.440 に答える
0

onOptionsItemSelected* 1 *という別のメソッドがあるのはなぜですか?

アクション アイテムに反応するすべてのコードは、onOptionsItemSelectedにある必要があります。

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // App icon in action bar clicked; go home
            Intent intent = new Intent(this, MainScreen.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;

        case android.R.id.email_action_item:

            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
            (...)
        default:
            return super.onOptionsItemSelected(item);
    }
}
于 2012-05-04T12:03:27.070 に答える
0

2 つの onOptionsItemSelected があるためですか? まあ、1つは onOptionsItemSelected* 1 * と呼ばれ、それを実際のものにマージする必要があります。

于 2012-05-04T11:59:16.023 に答える
0

呼び出されるメソッドを作成してonOptionsItemSelected1()、メニューエントリを押してメールを送信したときにアンドロイドがそれを呼び出すことを期待することはできません。

このメソッドを元のメソッドにマージonOptionsItemSelected()し、正しいケースに入れます。

于 2012-05-04T12:00:53.137 に答える