Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android:onClick的父或祖先上下文中找不到方法onClickcrea(视图)_Java_Android - Fatal编程技术网

Java 在android:onClick的父或祖先上下文中找不到方法onClickcrea(视图)

Java 在android:onClick的父或祖先上下文中找不到方法onClickcrea(视图),java,android,Java,Android,当我点击onClick=“onClickcrea”它不工作时,我基本上无法理解。我还试着更改onClick的名称,但这并不重要。我得到这个错误: java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.

当我点击onClick=“onClickcrea”它不工作时,我基本上无法理解。我还试着更改onClick的名称,但这并不重要。我得到这个错误:

 java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'onClickcrea'
                                                                                     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
                                                                                     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                                     at android.view.View.performClick(View.java:5198)
                                                                                     at android.view.View$PerformClick.run(View.java:21147)
                                                                                     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:5417)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
以下是我的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"
    tools:context="purgimon.pt11_marc_purgimon.Registra"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:weightSum="1">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/botoUsuari"
                android:id="@+id/NUsuari"
                android:layout_weight="0.11"
                android:textSize="20dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/editText2"
                android:layout_weight="1" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="36dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/botoCorreu"
                android:id="@+id/Correu"
                android:textSize="20dp" />

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="32dp"
                android:id="@+id/editText3" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="34dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/botoTelefon"
                android:id="@+id/Correu"
                android:textSize="20dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="32dp"
                android:id="@+id/editText4" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.03"
            android:weightSum="1">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="36dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/botoContrasenya"
                android:id="@+id/Contrasenya"
                android:textSize="20dp"
                android:layout_weight="0.05" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.57">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/editTextContra" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.03" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/botoRepeteixContrasenya"
                android:id="@+id/ContrasenyRepetida"
                android:textSize="20dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editTextContraR" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="36dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/Botosexe"
                android:id="@+id/Sexe"
                android:textSize="20dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.31"
            android:weightSum="1">

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="@string/BotoHome"
                android:id="@+id/Home"
                android:checked="false"
                android:layout_weight="0.08" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="@string/botoDona"
                android:id="@+id/Dona"
                android:layout_weight="0.12"
                android:checked="false"
                android:layout_marginLeft="150dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="368dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="@string/boto2Ajuda"
                android:id="@+id/AjudaRegistra"
                android:layout_marginLeft="30dp"
                android:onClick="ajuda"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="@string/botoCrea"
                android:id="@+id/onClickcrea"
                android:layout_marginLeft="120dp"
                android:onClick="onClickcrea" />
        </LinearLayout>

    </LinearLayout>

我个人建议使用onClickListener而不是XML属性。

我个人建议使用onClickListener而不是XML属性。

在视图膨胀的活动中找不到android:onClick中定义的方法。 此错误最常见的错误是方法名输入错误和布局错误


如其他评论中所述,您应该使用onClick侦听器。

在视图膨胀的活动中找不到android:onClick中定义的方法。 此错误最常见的错误是方法名输入错误和布局错误


正如其他评论中所述,您应该使用onClick监听器。

这就是为什么我从不使用
onClick
属性。直接在代码中设置一个
OnClickListener
,永远不会失败,也不会依赖任何神奇的反射技巧,这些技巧可以被各种各样的东西(比如Proguard)打破。点击按钮
AjudaRegistra
AjudaRegistra
是否有效。我的android应用程序有三个活动:我的主要活动称为“活动负责人”、“活动注册人”和“ajuda”。前两个活动有一个“ajuda”按钮,只有它在我的主活动的onClick方法中起作用。显然,我检查了方法和onClick是否具有相同的名称,还更改了ajuda的onClick名称以尝试是否有效,但任何更改都解决了问题。这就是为什么我从未使用
onClick
属性的原因。直接在代码中设置一个
OnClickListener
,永远不会失败,也不会依赖任何神奇的反射技巧,这些技巧可以被各种各样的东西(比如Proguard)打破。点击按钮
AjudaRegistra
AjudaRegistra
是否有效。我的android应用程序有三个活动:我的主要活动称为“活动负责人”、“活动注册人”和“ajuda”。前两个活动有一个“ajuda”按钮,只有它在我的主活动的onClick方法中起作用。显然,我检查了方法和onClick是否具有相同的名称,并且我还更改了ajuda的onClick名称以尝试是否有效,但是任何更改都解决了问题。
public class Registra extends AppCompatActivity {
    String contrasenya1;
    String contrasenya2;

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_principal, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void ajuda(View v) {
            Intent intent = new Intent(this, Ajuda.class);
            startActivity(intent);


    }

    public void onClickcrea (View view){
        EditText textContra1 =(EditText) findViewById(R.id.editTextContra);
        contrasenya1 = textContra1.getText().toString();
        EditText textContra2 = (EditText) findViewById(R.id.editTextContraR);
        contrasenya2 = textContra2.getText().toString();

        Toast missatge = Toast.makeText(getApplicationContext(), "Les contrasenyes han de coincidir!", Toast.LENGTH_SHORT);
        missatge.show();
    }
}