我创建了一个java文件,应该允许用户点击图像并加载另一个布局(超级简单的代码 - 但请记住我有点像菜鸟)但每次点击它我得到一个强制关闭错误消息,虽然一切似乎都对我(显示我所知道的)
我知道这可能是一件非常简单的事情 - 或者是一个类型的问题。
非常感谢任何帮助!
JAVA
package com.example.linkingmanager;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.content.Intent;
import android.view.View; import android.view.View.OnClickListener;
public class App2Activity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
public class Test extends Activity {
public void imageClick(View view) {
//Implement image click function
Intent intent = new Intent(App2Activity.this, AppActivity.class);
App2Activity.this.startActivity(intent);
}
}
;
}
logcat的
03-06 16:55:06.709: E/Trace(1634): error opening trace file: No such file or directory (2)
03-06 16:55:07.279: D/dalvikvm(1634): GC_FOR_ALLOC freed 54K, 8% free 2456K/2652K, paused 47ms, total 50ms
03-06 16:55:07.319: I/dalvikvm-heap(1634): Grow heap (frag case) to 5.831MB for 3456016-byte allocation
03-06 16:55:07.479: D/dalvikvm(1634): GC_FOR_ALLOC freed 2K, 4% free 5829K/6028K, paused 152ms, total 152ms
03-06 16:55:07.579: D/dalvikvm(1634): GC_CONCURRENT freed <1K, 4% free 5828K/6028K, paused 10ms+16ms, total 100ms
03-06 16:55:08.209: D/libEGL(1634): loaded /system/lib/egl/libEGL_emulation.so
03-06 16:55:08.209: D/(1634): HostConnection::get() New Host Connection established 0x2a1727a0, tid 1634
03-06 16:55:08.289: D/libEGL(1634): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-06 16:55:08.299: D/libEGL(1634): loaded /system/lib/egl/libGLESv2_emulation.so
03-06 16:55:08.499: W/EGL_emulation(1634): eglSurfaceAttrib not implemented
03-06 16:55:08.569: D/OpenGLRenderer(1634): Enabling debug mode 0
03-06 16:55:11.969: W/EGL_emulation(1634): eglSurfaceAttrib not implemented
03-06 16:55:18.629: D/AndroidRuntime(1634): Shutting down VM
03-06 16:55:18.629: W/dalvikvm(1634): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-06 16:55:18.659: E/AndroidRuntime(1634): FATAL EXCEPTION: main
03-06 16:55:18.659: E/AndroidRuntime(1634): java.lang.IllegalStateException: Could not find a method imageClick(View) in the activity class com.example.linkingmanager.App2Activity for onClick handler on view class android.widget.ImageView with id 'imageView1'
03-06 16:55:18.659: E/AndroidRuntime(1634): at android.view.View$1.onClick(View.java:3586)
03-06 16:55:18.659: E/AndroidRuntime(1634): at android.view.View.performClick(View.java:4204)
03-06 16:55:18.659: E/AndroidRuntime(1634): at android.view.View$PerformClick.run(View.java:17355)
03-06 16:55:18.659: E/AndroidRuntime(1634): at android.os.Handler.handleCallback(Handler.java:725)
03-06 16:55:18.659: E/AndroidRuntime(1634): at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 16:55:18.659: E/AndroidRuntime(1634): at android.os.Looper.loop(Looper.java:137)
03-06 16:55:18.659: E/AndroidRuntime(1634): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-06 16:55:18.659: E/AndroidRuntime(1634): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 16:55:18.659: E/AndroidRuntime(1634): at java.lang.reflect.Method.invoke(Method.java:511)
03-06 16:55:18.659: E/AndroidRuntime(1634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-06 16:55:18.659: E/AndroidRuntime(1634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-06 16:55:18.659: E/AndroidRuntime(1634): at dalvik.system.NativeStart.main(Native Method)
03-06 16:55:18.659: E/AndroidRuntime(1634): Caused by: java.lang.NoSuchMethodException: imageClick [class android.view.View]
03-06 16:55:18.659: E/AndroidRuntime(1634): at java.lang.Class.getConstructorOrMethod(Class.java:460)
03-06 16:55:18.659: E/AndroidRuntime(1634): at java.lang.Class.getMethod(Class.java:915)
03-06 16:55:18.659: E/AndroidRuntime(1634): at android.view.View$1.onClick(View.java:3579)
03-06 16:55:18.659: E/AndroidRuntime(1634): ... 11 more
答案 0 :(得分:2)
您正在布局文件(onClick
)中设置R.layout.main2
属性:
android:onClick="imageClick"
此布局是App2Activity
视图层次结构的一部分。因此,您必须在imageClick(View)
:
App2Activity
方法
public class App2Activity extends Activity {
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
public void imageClick(View view) {
Intent intent = new Intent(App2Activity.this, AppActivity.class);
App2Activity.this.startActivity(intent);
}
}
而不是Test
类。
答案 1 :(得分:1)
为什么要在另一个活动中定义另一个活动。
public class App2Activity extends Activity {
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
button.setOnClickListener(buttonListener);
}
private OnClickListener buttonListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(App2Activity.this, AppActivity.class);
App2Activity.this.startActivity(intent);
}
};
}