当按下一个按钮来切换意图时,我得到了芝加哥火车追踪器意外停止和一个logcat:
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
我的代码是:
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);
}}
和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>
答案 0 :(得分:1)
异常似乎很清楚:
java.lang.IllegalStateException:在活动类com.dev.chicagotraintracker.MainActivity中找不到方法sendMessage(View)`
错误是因为您在android:onClick="sendMessage"
中定义的ImageButton
中提到了activity_main.xml
,但您的sendMessage()
中没有任何MainActivity
功能。这就是你得到错误的原因。您需要在MainActivity
中定义该函数,告诉它在单击ImageButton
时应该发生什么。所以在该函数内部按钮点击时应执行的代码。
例如:如果您想在按钮上开始新活动,请执行以下操作:
public void sendMessage(View v){
Intent myIntent = new Intent(MainActivity.this, Screenmap.class);
startActivityForResult(myIntent, 0);
}
将这段代码放在onCreate()
之外,作为一个不同的函数。
答案 1 :(得分:1)
您必须在名为sendMessage
的活动中实施一种方法,因为logcat
说
您MainActivity.java
public void sendMessage(View)
{
\\ do your stuff
}
以及下一个错误也是因为这个
<activity android:name="Screenmap" android:label="Screenmap"></activity>
应该是这样的
<activity android:name=".Screenmap" android:label="Screenmap"></activity>
<activity android:name="com.dev.chicagotraintracker.Screenmap" android:label="Screenmap">
</activity>
不要忘记android:name=".Screenmap"
“。”在活动名称之前创建类的完整路径
答案 2 :(得分:0)
您的activity_main.xml指定在单击ImageButton时,它应调用sendMessage
方法,但是,您尚未在活动中创建sentMessage方法。
在主活动中将方法实现为public void sendMessage(View view){ // code }
时,同样重要的是要注意View
参数是对单击的ImageButton的引用。