【问题标题】:onClick error - AndroidonClick 错误 - Android
【发布时间】:2014-09-27 16:11:55
【问题描述】:

我对此很陌生。我的按钮没有响应点击。我将 android:onClick="onClick" 添加到 xml 文件中。现在我得到下面的错误。

java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class testapp.two.MainActivity for onClick handler on view class android.widget.Button with id 'buttonMinus'

请帮忙,Java 代码连同清单文件和主 xml 都包含在下面。

谢谢。

package testapp.two;

import testapp.two.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener{
    Button okButton, minusButton, plusButton;
    TextView textScore, scoreCard;
    int score = 95;
    private static final String TAG = "GolfScore";

    /** Called when the activity is first created. */

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

        Log.d(TAG, "onCreate started"); 

    okButton = (Button)findViewById(R.id.buttonOK);
    minusButton = (Button)findViewById(R.id.buttonMinus);
    plusButton = (Button)findViewById(R.id.buttonPlus);
    textScore = (TextView)findViewById(R.id.textScore);
    scoreCard = (TextView)findViewById(R.id.scoreCard);


    //set button listeners
    okButton.setOnClickListener(this);
    minusButton.setOnClickListener(this);
    plusButton.setOnClickListener(this);

    textScore.setText(String.valueOf(score));


    Log.d(TAG, "onCreate finished");
    }//onCreate

@Override   
    public void onClick(View v) {


        Log.d(TAG, "in onCreate");

        switch (v.getId()){
        case R.id.buttonMinus:
            score--;
            textScore.setText(String.valueOf(score));
            break;
        case R.id.buttonPlus:
            score++;
            textScore.setText(String.valueOf(score));
            break;

        case R.id.buttonOK:


            break;
        }//end of switch

    }//end of my onclick


}//end of MainActivity 
<RelativeLayout 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:onClick="@+id/buttonMinus, @+id/buttonPlus, @+id/textScore"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="false"
        android:layout_centerVertical="false"
        android:text="Golf Score App"
        android:textAlignment="center" />

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/title"
        android:layout_marginTop="14dp"
        android:baselineAligned="false"
        android:gravity="fill"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/buttonMinus"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="fill"
            android:layout_marginLeft="5dp"
            android:clickable="true"
            android:onClick="onClick"
            android:layout_marginTop="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="-" />

        <Button
            android:id="@+id/buttonPlus"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="fill"
            android:layout_marginLeft="0dp"
            android:clickable="true"
            android:onClick="onClick"
            android:layout_weight="1"
            android:text="+" />

        <TextView
            android:id="@+id/textScore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="fill"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="92"
            android:textSize="20sp" />

        <Button
            android:id="@+id/buttonOK"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="fill"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="33dp"
            android:clickable="true"
            android:onClick="onClick"
            android:layout_weight="1"
            android:text="Ok"
            android:textSize="15sp" />

    </LinearLayout>

    <TextView
        android:id="@+id/scoreCard"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/LinearLayout1"
        android:layout_marginTop="22dp"
        android:text="Score card info goes here" />

</RelativeLayout>

【问题讨论】:

  • 我不是 100% 确定,但我认为当您在 xml 中使用 android:onClick="onClick" 时,您不必在您的 MainActivity 中实现 onClickListener

标签: android android-layout


【解决方案1】:

正如 0xDEADC0DE 所说,如果您在 XML 中使用 onClick 属性,则不需要 OnClickListener,反之亦然。

onClick(View v)方法找不到的原因是Overrideannotation。这取决于OnClickListener 这种方式。有一个“免费”的方法,不需要注解,如果使用了onClick标签就可以找到该方法。

现在您有两个选择:摆脱 OnClicklistener 以及与 XML 中的按钮或 onClick 的关联。

编辑:

从RelativeLayout 和XML 中的按钮中删除onClick 属性。此外,从您的按钮中删除 clickable。如果设置clickable = true,View 将自动获得一个空的 OnClickListener。这意味着您的 Activity 中的 OnClickListener 不会被调用。

【讨论】:

  • 我以前的 XML 中没有 onClick。但是当我点击按钮时什么也没发生。
  • 谢谢史蒂夫,你说什么了。错误消失了,但是当我单击按钮时没有任何反应。
【解决方案2】:

只需从您的 XML 中删除 android:onClick="onClick"

请注意,最好为每个按钮创建一个OnClickListener,而不是一个必须检查id 才能决定做什么的大OnClickListener

【讨论】:

  • 呃,第二部分完全是意见。我更喜欢带有开关的单个 onClick 方法。
  • @Coeffect 我从 XML 中删除了“onClick”语句,但我的按钮仍然没有响应点击。任何帮助表示赞赏。
  • @fredo124 请用修改后的代码发布一个新问题。还要描述您在运行应用程序时遵循的确切步骤以及发生的情况。然后告诉我们您希望发生什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多