Reputation: 141
I have an android program that crashes and I get an error in 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
Here is my main activity:
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);
}
}
Here is my 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);
}
}
I have invoked the method sendMessage correctly in the MainActivity, where is the misspelled method defined?
Upvotes: 0
Views: 2309
Reputation: 495
In your layout xml file, you mis spelled sendMesage. Change
android:onClick="sendMesage"
to
android:onClick="sendMessage"
Upvotes: 1
Reputation: 5731
This is a small typo error. Inside your layout xml, you wrote for the button, android:onClick="sendMesage"
. But inside MainActivity
, wrote function as public void sendMessage(View view)
.
a small 's' missing ;)
Upvotes: 0