0

我创建了一个 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
4

2 回答 2

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课堂上。

于 2013-03-06T17:06:19.537 回答
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);
        }
    };
}
于 2013-03-06T16:58:43.110 回答