Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

android - "Could not find a method .. in the activity class .. for onClick handler"

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

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

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 file

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(Vi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

See the Documentation, in this example sendMessage method uses:

a View as its only parameter (this will be the View that was clicked).

Your error:

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'  

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...