不确定我的代码有什么问题

时间:2015-04-15 01:56:29

标签: java android

嘿,我正在回到android编程,正在做developer.google.com教程,我不知道这里有什么问题......

在" public void sendMessage(View view){"我知道错误&#39 ;;'预期

提前感谢您的帮助。

package com.apps.graham.myfirstapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;


public class MyActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.apps.graham.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //Called when the user clicks the send button next to the text field.
        if (id == R.id.action_settings) return true;
        {
        public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewByID(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

            return super.onOptionsItemSelected(item);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

技巧:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //Called when the user clicks the send button next to the text field.
    if (id == R.id.action_settings) {
        //sendMessage(your-view); here, if you want to do that.
        return true;
    }
        return super.onOptionsItemSelected(item);
}

并且您的新方法无法在另一种方法中定义:

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewByID(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}