无法链接OnClick方法

时间:2018-06-18 00:18:54

标签: android xml android-studio gradle

您好我不明白为什么我收到错误“相应的方法处理程序public playC(android.View.view)当我在Mainactivity.java文件中声明它时找不到。我尝试了一切但我不能似乎使它工作。 我是新手,所以请善待。

package com.londonappbrewery.xylophonepm;

import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;


public class MainActivity extends AppCompatActivity {

    // Helpful Constants
    private final int NR_OF_SIMULTANEOUS_SOUNDS = 7;
    private final float LEFT_VOLUME = 1.0f;
    private final float RIGHT_VOLUME = 1.0f;
    private final int NO_LOOP = 0;
    private final int PRIORITY = 0;
    private final float NORMAL_PLAY_RATE = 1.0f;

    // TODO: Add member variables here
    private SoundPool mSoundPool;
    private int mCSoundId;
    private int mDSoundId;
    private int mESoundId;
    private int mFSoundId;
    private int mGSoundId;
    private int mASoundId;
    private int mBSoundId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // TODO: Create a new SoundPool
        mSoundPool = new SoundPool(7,AudioManager.STREAM_MUSIC,0);


        // TODO: Load and get the IDs to identify the sounds
        public void playC(View v){
            Log.d("Xylophone","Red button clicked");
        }

    }

    //TODO: Add the play methods triggered by the button

}

这是activity_xml文件..请让我免于绝望。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        style="@style/KeyStyle"
        android:id="@+id/c_key"
        android:background="@color/red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="playC" />

    <Button
        style="@style/KeyStyle"
        android:id="@+id/d_key"
        android:background="@color/orange"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        style="@style/KeyStyle"
        android:id="@+id/e_key"
        android:background="@color/yellow"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        style="@style/KeyStyle"
        android:id="@+id/f_key"
        android:background="@color/green"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        style="@style/KeyStyle"
        android:id="@+id/g_key"
        android:background="@color/turquoise"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


    <Button
        style="@style/KeyStyle"
        android:id="@+id/a_key"
        android:background="@color/blue"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        style="@style/KeyStyle"
        android:id="@+id/b_key"
        android:background="@color/purple"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

playC移到onCreate之外,如下所示:

public class MainActivity extends AppCompatActivity {
    ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // TODO: Create a new SoundPool
        mSoundPool = new SoundPool(7,AudioManager.STREAM_MUSIC,0);

    }

    public void playC(View v){
        Log.d("Xylophone","Red button clicked");
    }
}