“在活动类中找不到方法 .. 用于 onClick 处理程序" [英] "Could not find a method .. in the activity class .. for onClick handler"

查看:19
本文介绍了“在活动类中找不到方法 .. 用于 onClick 处理程序"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个 android 项目.我正在使用 thinkgear api.我有两个活动.我有一个意图在第一个活动中调用以启动第二个活动.在我的第二个活动中,我有一个播放声音的按钮和一个暂停声音的按钮.但是当我尝试在第二个活动中播放声音时,我的应用程序结束了.请指出错误.我正在附加 java 文件和 logcat 文件.

I am working on my first android project. I am using the thinkgear api. I have two activities. I have an intent in the first activity calling to start the second activity. In my second activity, I have a button to play a sound and a button to pause the sound. But when I try to play the sound in the second activity, my application ends. Please point out the mistakes. I am attaching both the java files and the logcat file.

mindwave1.java

mindwave1.java

package com.example.mindwave1;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
//import android.util.Log;
//import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.neurosky.thinkgear.*;

public class MainActivity extends Activity {
BluetoothAdapter bluetoothAdapter;

TextView tv;
Button b;

TGDevice tgDevice;
final boolean rawEnabled = true;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.textView1);
    tv.setText("");
    tv.append("Android version: " + Integer.valueOf(android.os.Build.VERSION.SDK_INT) +      "
" );
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if(bluetoothAdapter == null) {
        // Alert user that Bluetooth is not available
        Toast.makeText(this, "Bluetooth not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }else {
        /* create the TGDevice */
        tgDevice = new TGDevice(bluetoothAdapter, handler);
    }  
}

@Override
public void onDestroy() {
    tgDevice.close();
    super.onDestroy();
}
/**
 * Handles messages from TGDevice
 */
@SuppressLint("HandlerLeak")
private final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case TGDevice.MSG_STATE_CHANGE:

            switch (msg.arg1) {
                case TGDevice.STATE_IDLE:
                    break;
                case TGDevice.STATE_CONNECTING:                         
                    tv.append("Connecting...
");
                    break;                          
                case TGDevice.STATE_CONNECTED:
                    tv.append("Connected.
");
                    tgDevice.start();
                    break;
                case TGDevice.STATE_NOT_FOUND:
                    tv.append("Can't find
");
                    break;
                case TGDevice.STATE_NOT_PAIRED:
                    tv.append("not paired
");
                    break;
                case TGDevice.STATE_DISCONNECTED:
                    tv.append("Disconnected mang
");
            }

            break;
        //case TGDevice.MSG_POOR_SIGNAL:
                //signal = msg.arg1;
                //tv.append("PoorSignal: " + msg.arg1 + "
");
            //break;
        case TGDevice.MSG_RAW_DATA:   
                //raw1 = msg.arg1;
                //tv.append("Got raw: " + msg.arg1 + "
");
            break;
        case TGDevice.MSG_HEART_RATE:
            tv.append("Heart rate: " + msg.arg1 + "
");
            break;
        case TGDevice.MSG_ATTENTION:
                //att = msg.arg1;
                tv.setText("");
                tv.append("Test number 1 for attention and meditation
");
                tv.append("Attention: " + msg.arg1 + "
");
                //Log.v("HelloA", "Attention: " + msg.arg1 + "
");
            break;
        case TGDevice.MSG_MEDITATION:
                tv.append("Meditation: " + msg.arg1 +"
");
            break;
        case TGDevice.MSG_BLINK:
                tv.append("Blink: " + msg.arg1 + "
");
            break;
        case TGDevice.MSG_RAW_COUNT:
                //tv.append("Raw Count: " + msg.arg1 + "
");
            break;
        case TGDevice.MSG_LOW_BATTERY:
            Toast.makeText(getApplicationContext(), "Low battery!", Toast.LENGTH_SHORT).show();
            break;
        case TGDevice.MSG_RAW_MULTI:
            //TGRawMulti rawM = (TGRawMulti)msg.obj;
            //tv.append("Raw1: " + rawM.ch1 + "
Raw2: " + rawM.ch2);
        default:
            break;
    }
    }
};

public void nxt(View view)
{
    Intent intent = new Intent(this, SecondPageActivity.class);
    startActivity(intent);
}

public void doStuff(View view) {
    if(tgDevice.getState() != TGDevice.STATE_CONNECTING && tgDevice.getState() != TGDevice.STATE_CONNECTED)
        tgDevice.connect(rawEnabled);   
    //tgDevice.ena
}
}

SecondPageActivity.java

SecondPageActivity.java

package com.example.mindwave1;

import android.os.Bundle;
import android.media.MediaPlayer;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class SecondPageActivity extends Activity {

MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second_page);
    // Show the Up button in the action bar.
    setupActionBar();
}



public void play(){

    mp = MediaPlayer.create(SecondPageActivity.this, R.raw.music);
    mp.start();
}

/**
 * Set up the {@link android.app.ActionBar}.
 */
private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second_page, menu);
    return 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:
        //

        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

logcat 文件

04-16 16:29:50.676: D/dalvikvm(13458): GC_FOR_ALLOC freed 65K, 2% free 7676K/7784K, paused 22ms, total 22ms
04-16 16:29:50.676: I/dalvikvm-heap(13458): Grow heap (frag case) to 8.899MB for 1436984-byte allocation
04-16 16:29:50.716: D/dalvikvm(13458): GC_FOR_ALLOC freed 1K, 2% free 9078K/9188K, paused 37ms, total 37ms
04-16 16:29:50.746: V/TGDevice(13458): EKG path: /storage/emulated/0/Android/data/com.neurosky.thinkgear/files/EKG/parameters
04-16 16:29:50.746: D/TGDevice(13458): Initialized. Version: 9
04-16 16:29:50.806: D/libEGL(13458): loaded /system/lib/egl/libEGL_tegra.so
04-16 16:29:50.826: D/libEGL(13458): loaded /system/lib/egl/libGLESv1_CM_tegra.so
04-16 16:29:50.846: D/libEGL(13458): loaded /system/lib/egl/libGLESv2_tegra.so
04-16 16:29:50.866: D/OpenGLRenderer(13458): Enabling debug mode 0
04-16 16:29:57.736: V/mindwave1(13458): appln2 start
04-16 16:30:06.076: D/AndroidRuntime(13458): Shutting down VM
04-16 16:30:06.076: W/dalvikvm(13458): threadid=1: thread exiting with uncaught exception (group=0x41834ba8)
04-16 16:30:06.076: E/AndroidRuntime(13458): FATAL EXCEPTION: main
04-16 16:30:06.076: E/AndroidRuntime(13458): Process: com.example.mindwave1, PID: 13458
04-16 16:30:06.076: E/AndroidRuntime(13458): java.lang.IllegalStateException: Could not find a method play(View) in the activity class com.example.mindwave1.SecondPageActivity for onClick handler on view class android.widget.Button with id 'button1'
04-16 16:30:06.076: E/AndroidRuntime(13458):    at android.view.View$1.onClick(View.java:3810)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at android.view.View.performClick(View.java:4438)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at android.view.View$PerformClick.run(View.java:18422)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at android.os.Handler.handleCallback(Handler.java:733)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at android.os.Looper.loop(Looper.java:136)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at java.lang.reflect.Method.invoke(Method.java:515)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at dalvik.system.NativeStart.main(Native Method)
04-16 16:30:06.076: E/AndroidRuntime(13458): Caused by: java.lang.NoSuchMethodException: play [class android.view.View]
04-16 16:30:06.076: E/AndroidRuntime(13458):    at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at java.lang.Class.getMethod(Class.java:857)
04-16 16:30:06.076: E/AndroidRuntime(13458):    at android.view.View$1.onClick(View.java:3803)
04-16 16:30:06.076: E/AndroidRuntime(13458):    ... 11 more
04-16 16:35:06.416: D/dalvikvm(13736): GC_FOR_ALLOC freed 69K, 2% free 7676K/7788K, paused 24ms, total 24ms
04-16 16:35:06.416: I/dalvikvm-heap(13736): Grow heap (frag case) to 8.900MB for 1436984-byte allocation
04-16 16:35:06.446: D/dalvikvm(13736): GC_FOR_ALLOC freed 1K, 2% free 9078K/9192K, paused 25ms, total 25ms
04-16 16:35:06.466: V/TGDevice(13736): EKG path: /storage/emulated/0/Android/data/com.neurosky.thinkgear/files/EKG/parameters
04-16 16:35:06.466: D/TGDevice(13736): Initialized. Version: 9
04-16 16:35:06.516: D/libEGL(13736): loaded /system/lib/egl/libEGL_tegra.so
04-16 16:35:06.536: D/libEGL(13736): loaded /system/lib/egl/libGLESv1_CM_tegra.so
04-16 16:35:06.546: D/libEGL(13736): loaded /system/lib/egl/libGLESv2_tegra.so
04-16 16:35:06.566: D/OpenGLRenderer(13736): Enabling debug mode 0
04-16 16:39:46.226: D/dalvikvm(13927): GC_FOR_ALLOC freed 73K, 2% free 7676K/7788K, paused 20ms, total 21ms
04-16 16:39:46.236: I/dalvikvm-heap(13927): Grow heap (frag case) to 8.899MB for 1436984-byte allocation
04-16 16:39:46.256: D/dalvikvm(13927): GC_FOR_ALLOC freed 1K, 2% free 9078K/9192K, paused 22ms, total 22ms
04-16 16:39:46.296: V/TGDevice(13927): EKG path: /storage/emulated/0/Android/data/com.neurosky.thinkgear/files/EKG/parameters
04-16 16:39:46.296: D/TGDevice(13927): Initialized. Version: 9
04-16 16:39:46.366: D/libEGL(13927): loaded /system/lib/egl/libEGL_tegra.so
04-16 16:39:46.376: D/libEGL(13927): loaded /system/lib/egl/libGLESv1_CM_tegra.so
04-16 16:39:46.396: D/libEGL(13927): loaded /system/lib/egl/libGLESv2_tegra.so
04-16 16:39:46.416: D/OpenGLRenderer(13927): Enabling debug mode 0
04-16 16:39:50.416: V/mindwave1(13927): appln2 start
04-16 16:39:51.666: D/AndroidRuntime(13927): Shutting down VM
04-16 16:39:51.666: W/dalvikvm(13927): threadid=1: thread exiting with uncaught exception (group=0x41834ba8)
04-16 16:39:51.676: E/AndroidRuntime(13927): FATAL EXCEPTION: main
04-16 16:39:51.676: E/AndroidRuntime(13927): Process: com.example.mindwave1, PID: 13927
04-16 16:39:51.676: E/AndroidRuntime(13927): java.lang.IllegalStateException: Could not find a method play(View) in the activity class com.example.mindwave1.SecondPageActivity for onClick handler on view class android.widget.Button with id 'button1'
04-16 16:39:51.676: E/AndroidRuntime(13927):    at android.view.View$1.onClick(View.java:3810)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at android.view.View.performClick(View.java:4438)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at android.view.View$PerformClick.run(View.java:18422)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at android.os.Handler.handleCallback(Handler.java:733)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at android.os.Looper.loop(Looper.java:136)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at java.lang.reflect.Method.invoke(Method.java:515)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at dalvik.system.NativeStart.main(Native Method)
04-16 16:39:51.676: E/AndroidRuntime(13927): Caused by: java.lang.NoSuchMethodException: play [class android.view.View]
04-16 16:39:51.676: E/AndroidRuntime(13927):    at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at java.lang.Class.getMethod(Class.java:857)
04-16 16:39:51.676: E/AndroidRuntime(13927):    at android.view.View$1.onClick(View.java:3803)
04-16 16:39:51.676: E/AndroidRuntime(13927):    ... 11 more

推荐答案

参见Documentation,在本例中 sendMessage 方法使用:

See the Documentation, in this example sendMessage method uses:

一个 View 作为其唯一参数(这将是被点击的 View).

你的错误:

Could not find a method play(View) in the activity class com.example.mindwave1.SecondPageActivity for onClick handler on view class android.widget.Button with id 'button1'  

您需要在方法中使用 View 参数.解决办法:

You need to use the View parameter in your method. The solution:

public void play(View view){
    ...
}  

这篇关于“在活动类中找不到方法 .. 用于 onClick 处理程序"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆