“无法在活动类中找到方法..对于onClick处理程序”

时间:2014-04-16 23:42:18

标签: android onclick android-view

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

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) +      "\n" );
    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...\n");
                    break;                          
                case TGDevice.STATE_CONNECTED:
                    tv.append("Connected.\n");
                    tgDevice.start();
                    break;
                case TGDevice.STATE_NOT_FOUND:
                    tv.append("Can't find\n");
                    break;
                case TGDevice.STATE_NOT_PAIRED:
                    tv.append("not paired\n");
                    break;
                case TGDevice.STATE_DISCONNECTED:
                    tv.append("Disconnected mang\n");
            }

            break;
        //case TGDevice.MSG_POOR_SIGNAL:
                //signal = msg.arg1;
                //tv.append("PoorSignal: " + msg.arg1 + "\n");
            //break;
        case TGDevice.MSG_RAW_DATA:   
                //raw1 = msg.arg1;
                //tv.append("Got raw: " + msg.arg1 + "\n");
            break;
        case TGDevice.MSG_HEART_RATE:
            tv.append("Heart rate: " + msg.arg1 + "\n");
            break;
        case TGDevice.MSG_ATTENTION:
                //att = msg.arg1;
                tv.setText("");
                tv.append("Test number 1 for attention and meditation\n");
                tv.append("Attention: " + msg.arg1 + "\n");
                //Log.v("HelloA", "Attention: " + msg.arg1 + "\n");
            break;
        case TGDevice.MSG_MEDITATION:
                tv.append("Meditation: " + msg.arg1 +"\n");
            break;
        case TGDevice.MSG_BLINK:
                tv.append("Blink: " + msg.arg1 + "\n");
            break;
        case TGDevice.MSG_RAW_COUNT:
                //tv.append("Raw Count: " + msg.arg1 + "\n");
            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 + "\nRaw2: " + 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

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

3 个答案:

答案 0 :(得分:4)

请参阅Documentation,在此示例中sendMessage方法使用:

  

将视图作为唯一参数(这将是单击的视图)。

您的错误:

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参数。解决方案:

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

答案 1 :(得分:2)

你的方法

public void play(){

缺少参数

public void play(View v){

答案 2 :(得分:2)

日志或其他东西的哇格式。你的问题是

  

无法在活动类中找到方法播放(View)       com.example.mindwave1.SecondPageActivity for view类的onClick处理程序       带有id' button1'的android.widget.Button 04-16 16:39:51.676:E / AndroidRuntime(13927):

你可能有一个功能" play"在onclick xml属性上定义。此函数也在视图中传递,因此您的方法签名需要看起来像

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