Android中的onClick方法

时间:2017-02-21 16:46:35

标签: android android-studio

从不使用方法(calculate.view.View

android crush错误:

  

---------崩溃的开始           E / AndroidRuntime:致命异常:主要                             过程:...,PID:2537                             java.lang.IllegalStateException:找不到父或祖先的方法计算(MainActivity)(View)上下文中的android:onClick属性在视图类android.support.v7.widget.AppCompatButton上定义,id为'button'                                 在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)                                 在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)                                 在android.view.View.performClick(View.java:5198)                                 在android.view.View $ PerformClick.run(View.java:21147)                                 在android.os.Handler.handleCallback(Handler.java:739)                                 在android.os.Handler.dispatchMessage(Handler.java:95)                                 在android.os.Looper.loop(Looper.java:148)                                 在android.app.ActivityThread.main(ActivityThread.java:5417)                                 at java.lang.reflect.Method.invoke(Native Method)                                 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)           申请已终止。

什么是挤压错误?

** AndroidManifest:**

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="...">


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".CalculatorActivity$CalculatorActivity" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

** CalculatorActivity:**

package ...;


import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CalculatorActivity extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.calculator_main, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().setTitle("CrCal");
    }




    }

1 个答案:

答案 0 :(得分:0)

  

在父级或祖先的上下文中找不到方法计算(MainActivity)(View)for android:onClick

此错误表示您的活动不包含您在按钮布局中定义的方法。因此,当您单击该按钮时,我认为您的应用程序崩溃了。检查你的布局,以确保你在android中定义的方法:onClick =&#34;计算&#34;实际存在于您的MainActivity中。