Reputation: 10223
When pressing a button to switch intents, I get the Chicago Train Tracker has stopped unexpectedly and a logcat of:
09-02 19:54:15.745: D/AndroidRuntime(20681): Shutting down VM
09-02 19:54:15.745: W/dalvikvm(20681): threadid=1: thread exiting with uncaught exception (group=0x41ab6700)
09-02 19:54:15.755: E/AndroidRuntime(20681): FATAL EXCEPTION: main
09-02 19:54:15.755: E/AndroidRuntime(20681): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.dev.chicagotraintracker.MainActivity for onClick handler on view class android.widget.ImageButton with id 'Button'
09-02 19:54:15.755: E/AndroidRuntime(20681): at android.view.View$1.onClick(View.java:3620)
09-02 19:54:15.755: E/AndroidRuntime(20681): at android.view.View.performClick(View.java:4240)
09-02 19:54:15.755: E/AndroidRuntime(20681): at android.view.View$PerformClick.run(View.java:17721)
09-02 19:54:15.755: E/AndroidRuntime(20681): at android.os.Handler.handleCallback(Handler.java:730)
09-02 19:54:15.755: E/AndroidRuntime(20681): at android.os.Handler.dispatchMessage(Handler.java:92)
09-02 19:54:15.755: E/AndroidRuntime(20681): at android.os.Looper.loop(Looper.java:137)
09-02 19:54:15.755: E/AndroidRuntime(20681): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-02 19:54:15.755: E/AndroidRuntime(20681): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 19:54:15.755: E/AndroidRuntime(20681): at java.lang.reflect.Method.invoke(Method.java:525)
09-02 19:54:15.755: E/AndroidRuntime(20681): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-02 19:54:15.755: E/AndroidRuntime(20681): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-02 19:54:15.755: E/AndroidRuntime(20681): at dalvik.system.NativeStart.main(Native Method)
09-02 19:54:15.755: E/AndroidRuntime(20681): Caused by: java.lang.NoSuchMethodException: sendMessage [class android.view.View]
09-02 19:54:15.755: E/AndroidRuntime(20681): at java.lang.Class.getConstructorOrMethod(Class.java:423)
09-02 19:54:15.755: E/AndroidRuntime(20681): at java.lang.Class.getMethod(Class.java:787)
09-02 19:54:15.755: E/AndroidRuntime(20681): at android.view.View$1.onClick(View.java:3613)
09-02 19:54:15.755: E/AndroidRuntime(20681): ... 11 more
09-02 19:54:16.875: I/Process(20681): Sending signal. PID: 20681 SIG: 9
My code is:
MainActivity.java
package com.dev.chicagotraintracker;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}
public void login_Click(View v) {;
Intent myIntent = new Intent(MainActivity.this, Screenmap.class);
startActivityForResult(myIntent, 0);
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
;
}
Screenmap.java
package com.dev.chicagotraintracker;
import android.os.Bundle;
import android.app.Activity;
public class Screenmap extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screenmap);
}}
and AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dev.chicagotraintracker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.dev.chicagotraintracker.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Screenmap" android:label="Screenmap"></activity>
</application>
</manifest>
Upvotes: 0
Views: 1904
Reputation: 26007
The Exception seems to be clear:
java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.dev.chicagotraintracker.MainActivity `
The error is because you have mentioned android:onClick="sendMessage"
in the ImageButton
defined in activity_main.xml
, but you don't have any sendMessage()
function in your MainActivity
. That is why you are getting the error . You need to define that function in the MainActivity
which tell it what should happen when the ImageButton
is clicked. So the code which should execute on button click, inside that function.
Eg: If you want to start a new activity on Button click do something like:
public void sendMessage(View v){
Intent myIntent = new Intent(MainActivity.this, Screenmap.class);
startActivityForResult(myIntent, 0);
}
Put this piece of code outside the onCreate()
and as a different function.
Upvotes: 1
Reputation: 11314
you must implement a method in your activity with name sendMessage
as your logcat
says
something like this in Your MainActivity.java
public void sendMessage(View)
{
\\ do your stuff
}
and also your next error would be because of this
<activity android:name="Screenmap" android:label="Screenmap"></activity>
it should be like this
<activity android:name=".Screenmap" android:label="Screenmap"></activity>
<activity android:name="com.dev.chicagotraintracker.Screenmap" android:label="Screenmap">
</activity>
do not forget android:name=".Screenmap"
"." before activity name that makes full path of class
Upvotes: 1
Reputation: 433
Your activity_main.xml specifies that when the ImageButton is clicked, it should call the sendMessage
method, however, you have not created a sentMessage method in your activity.
When you implement the method as public void sendMessage(View view){ // code }
in your main activity, it's also important to note that the View
parameter is a reference to the ImageButton that was clicked.
Upvotes: 0