Java:java.lang.IllegalStateException:找不到方法:

时间:2013-12-26 17:46:49

标签: android illegalstateexception

我有一个崩溃的android程序,我在LogCat中收到错误:

E/AndroidRuntime(21504): FATAL EXCEPTION: main
E/AndroidRuntime(21504): java.lang.IllegalStateException: Could not find a 
method sendMesage(View) in the activity class com.example.test.MainActivity 
for onClick handler on view class android.widget.Button

这是我的主要活动:

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.test.MESSAGE";
        @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;
        }

        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);
        }
    }

这是我的DisplayMessageActivity:

public class DisplayMessageActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
        // Show the Up button in the action bar.
        setupActionBar();

        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(textView);
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/
                    // navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我在MainActivity中正确调用了sendMessage方法,定义了拼写错误的方法在哪里?

2 个答案:

答案 0 :(得分:1)

在你的布局xml文件中,拼写错误的sendMesage。变化

android:onClick="sendMesage"

android:onClick="sendMessage"

答案 1 :(得分:0)

这是一个小错字错误。在你的布局xml中,你写了一个按钮,android:onClick="sendMesage"。但在MainActivity内,将函数写为public void sendMessage(View view)

一个小'的'缺失;)