2016-06-04 3 views
1

Ich kann im Grunde nicht verstehen, wenn ich auf onClick = "onClickcrea" klicke, funktioniert es nicht. Ich habe auch versucht, den Namen des onClick zu ändern, aber das spielt keine Rolle. Ich bekomme diese Fehlermeldung:Konnte Methode onClickcrea (View) in einem Elternteil oder Vorfahren nicht finden Kontext für android: 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) 

hier ist meine xml Aktivität:

<?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> 

Und hier mein Java-Code:

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(); 
    } 
} 
+0

Deshalb verwende ich nie das 'onClick' Attribut. Einen 'OnClickListener' direkt im Code zu setzen, wird niemals fehlschlagen und beruht nicht auf irgendwelchen magischen Reflektionstricks, die durch eine Vielzahl von Dingen (wie Proguard) gebrochen werden können. – Karakuri

+0

Funktioniert 'ajuda (View)' durch Klicken auf den Button 'AjudaRegistra'? – sakiM

+0

nein, tut es nicht. Meine Android-Anwendung hat drei Aktivitäten: Meine Hauptaktivität namens "activity_principal", "activity_registra" und "ajuda". Die ersten beiden Aktivitäten haben einen "Ajuda" -Button und nur dieser funktioniert in meiner onClick-Methode in meiner Hauptaktivität. Offensichtlich habe ich überprüft, dass die Methode und das onClick den gleichen Namen haben und ich habe auch den ajudas onClick-Namen geändert, um zu versuchen, ob es funktioniert, aber jede Änderung löste das Problem. –

Antwort

0

Persönlich würde ich vorschlagen, eine OnClickListener anstelle des XML-Attribut .

0

Die Methode, die in android: onClick definiert wurde, konnte nicht in der Aktivität gefunden werden, in der die Ansicht aufgebläht wurde. Die häufigsten Fehler für diesen Fehler sind Methodenname typo und falsche Layoutinflation.

Wie in anderen Kommentaren erwähnt, sollten Sie lieber onClickListeners verwenden.

Verwandte Themen