2017-05-26 5 views
-1

Ich versuche, onclick zu implementieren, wenn ich auf die erste Schaltfläche klicke, um zu einem neuen Fenster zu gehen, aber wenn ich auf es klicke, bricht Programm ab. Ich habe den Fehler:ButtonClick bricht Programm

W/OpenGLRenderer: Fehler beim Festlegen von EGL_SWAP_BEHAVIOR auf der Oberfläche 0x97154160, error = EGL_BAD_MATCH.

Dies ist Haupt:

package FirstApp.myapplication; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.content.Intent; 
import android.widget.Button; 
import android.widget.TextView; 


public class MainActivity extends AppCompatActivity implements 
View.OnClickListener{ 
TextView text; 
Button button; 
Button button2; 
boolean onClick = true; 

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


    button = (Button) findViewById(R.id.button); 


} 
@Override 

public void onClick(View view) { 


    if(view.getId() == R.id.button){ 

     text.setText("Prvi button je kliknut"); 

    } 

    else if(view.getId() == R.id.button2){ 

     /* Intent i = new Intent(this, DetailActivity.class); 
     i.putExtra("TEXT", input.getText().toString()); 
     startActivity(i);/*/ 

     text.setText("Drugi button je kliknut"); 
    } 
    } 
} 

Und das ist zweite Klasse:

package dvino.myapplication; 

import android.app.Activity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 


/** 
    * Created by zeroOne on 26.5.2017.. 
*/ 

public class Dvino extends Activity{ 
@Override 
protected void onCreate(Bundle saveInstanceState){ 

    super.onCreate(saveInstanceState); 
    setContentView(R.layout.dvino); 
    } 


} 

EDIT-dies ist Fehlermeldung, die ich erhalten speichert, wenn das Programm abstürzt:

<'E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: dvino.myapplication, PID: 3044 
        java.lang.IllegalStateException: Could not find method 
    OnClick(View) in a parent or ancestor Context for android:onClick attribute 
     defined on view class android.support.v7.widget.AppCompatButton with id 
    'button' 
         at 


android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) 
         at 


    android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
         at android.view.View.performClick(View.java:5610) 
         at android.view.View$PerformClick.run(View.java:22265) 
         at android.os.Handler.handleCallback(Handler.java:751) 
         at android.os.Handler.dispatchMessage(Handler.java:95) 
         at android.os.Looper.loop(Looper.java:154) 
         at android.app.ActivityThread.main(ActivityThread.java:6077) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)' 

Haupt XML-Datei:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    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="dvino.myapplication.MainActivity"> 


    <Button 
     android:text="D'vino" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button" 
     android:layout_marginLeft="18dp" 
     android:layout_alignBottom="@+id/button2" 
     android:layout_alignParentEnd="true" 
     android:layout_marginBottom="49dp" 
     android:onClick="OnClick" 
     /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/dvino" 
     android:id="@+id/imageView" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

    <Button 
     android:text="D'vino Kitchen" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/button2" 
     android:layout_marginLeft="18dp" 
     android:layout_marginBottom="12dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:onClick="onClick" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/title_message" 
     android:id="@+id/text" 
     android:textAppearance="?android:textAppearanceLarge" 
     android:layout_below="@+id/imageView" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="31dp" /> 


</RelativeLayout> 
+1

Java ist Case-Sensitive. 'android: onClick =" OnClick "' ** stimmt nicht überein ** 'public void onClick (Ansicht anzeigen) {' –

Antwort

2

Ihr Programm stürzt wegen einer Nullzeigerausnahme ab, wenn Sie versuchen, textview.setText() aufzurufen - textview ist null, weil Sie es noch nicht initialisiert haben.

auf die gleiche Weise button = (Button)findView... hast du haben, dass auch

+0

Nein, es bricht immer noch, nachdem ich es initialisiert habe – ZeroOne

+0

Ok, Sie haben jetzt einen separaten Fehler. In Ihrer XML-Datei ist ein Fehler enthalten. Kannst du das posten? – Shmuel

+0

Ich habe Update mit XML-Datei – ZeroOne

0

Ihr Code haben Textview Probleme und diese Teile für die Textview zu tun: public class MainActivity erweitert AppCompatActivity implementiert View.OnClickListener {} Änderung auf: public class MainActivity erweitert Aktivität implementiert View.OnClickListener {

bearbeiten --- Okey.Try es.

public class MainActivity erstreckt Aktivität {

private Button button; 
private TextView text; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    button=(Button) findViewById(R.id.testB); 
    text=(TextView) findViewById(R.id.testT); 


    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      button.setText("ClickedButton"); 
      text.setText("ChangedText"); 
     } 
    }); 


} 

}

+0

Wenn ich das tue sagt es - Kann Symbol nicht auflösen Aktivität – ZeroOne

+0

Jetzt sehe ich Ihre XML-Datei. Wenn Sie den oberen Code verwenden können, entfernen Sie diese Zeile android: onClick = "OnClick" – iOSDEV

0

ändern onclick="OnClick" Um onclick="onClick" für den Button-Widget mit ID = "button"

Verwendung Großbuchstaben sorgfältig Denken Sie daran;)

0

In Ihrer onCreate Methode nach der Initialisierung n der Taste button=(Button)findViewBy... setzen button.onClicklistener(this);

0

java.lang.IllegalStateException: Es konnte kein Verfahren OnClick(View) in einem Elternteil oder Vorfahr Context für Attribut finden definiert auf Ansichtsklasse android.support.v7.widget.AppCompatButton mit id 'button'

1. Ändern Sie in Ihrem Layout XML das Button-Attribut Wert auf onClick.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    .......... 
    ............... 
    tools:context="dvino.myapplication.MainActivity"> 

    <Button   
     android:id="@+id/button" 
     .......... 
     ................. 
     android:onClick="onClick" /> 


    <Button 
     android:id="@+id/button2" 
     .......... 
     ................ 
     android:onClick="onClick" /> 

    ........... 
    ................. 

</RelativeLayout> 

2. In Ihrem MainActivity, versuchen Sie auf ein Null TextView Objekt zu setzen, da Sie nicht die reference davon von Ihrem angefügten layout erhalten haben. Es kann eine NullPointerException verursachen. Um dies zu vermeiden Ausnahme wie unten Ihre Aktivität onCreate() Methode aktualisieren:

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

    button = (Button) findViewById(R.id.button); 
    text = (TextView) findViewById(R.id.text); 
} 

Hope this helfen ~

Verwandte Themen