Android无法找到我的onClick方法 [英] Android unable to find my onClick method

查看:133
本文介绍了Android无法找到我的onClick方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,当我单击该按钮时,在片段的布局XML中声明的我的Button(id i_am_a_problem)生成错误. Android尝试调用onClick方法public void calculate(View v),但是找不到它(尽管在MainActivity中声明了它).我的代码的详细信息在下面,并且错误是在那之后的.请帮助我确定为什么android无法找到onClick方法.谢谢.

I am having an issue where my Button (id i_am_a_problem) which is declared in the fragment's layout XML, is generating an error when the button is clicked. Android tries to call the onClick method public void calculate(View v) but is unable to find it (despite it being declared in MainActivity). Details of my code are below, and the error is after that. Please help me determine why android is unable to find the onClick method. Thanks.

此代码中实际上没有其他任何事情(新项目)

There is really not anything else going on in this code (fresh new project)

MainActivity.java

MainActivity.java

package supercali.FRAG.alistic;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) { /*snip - irrelevance*/ }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) { /*snip - irrelevance*/ }

    public void calculcate(View v){
        TextView tv = (TextView)findViewById(R.id.results);
        tv.setText(calculate_value());
    }

    private String calculate_value(){
        return "Abra Kadabra";
    }
}

MainActivity的布局仅包含一个片段

MainActivity's layout just contains a fragment

<fragment
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/fragment"
  android:name="supercali.FRAG.alistic.MainActivityFragment" 
  tools:layout="@layout/fragment_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

该片段的布局为:

<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:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context=".MainActivityFragment">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/calculate"
        android:onClick="calculate"
        android:id="@+id/i_am_a_problem"/>

    <TextView android:text="@string/results_pending" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/results"/>

</LinearLayout>

问题按钮就在其中^^,ID为i_am_a_problem

LogCat:

06-18 09:49:41.280  31642-31642/supercali.FRAG.alistic E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: supercali.FRAG.alistic, PID: 31642
    java.lang.IllegalStateException: Could not find method calculate(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
            at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4441)
            at android.view.View$DeclaredOnClickListener.onClick(View.java:4405)
            at android.view.View.performClick(View.java:5147)
            at android.view.View$PerformClick.run(View.java:21069)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5401)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)

推荐答案

您拼写了错误的方法. XML正在寻找 calculate ,但您的方法是 calculate

You misspelled the method. the XML is looking for calculate but your method is calculcate

这篇关于Android无法找到我的onClick方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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